필터 지우기
필터 지우기

how to access the cell array ?

조회 수: 1 (최근 30일)
joha
joha 2015년 10월 10일
댓글: Star Strider 2015년 10월 10일
patients =
'patient' 'status' 'genotype' 'genotype'
[ 1] [ 1] 'A' 'C'
[ 1] [ 1] 'E' 'F'
[ 2] [ 1] 'B' 'D'
[ 2] [ 1] 'E' 'G'
[ 3] [ 0] 'D' 'G'
[ 3] [ 0] 'I' 'K'
[ 4] [ 1] 'C' 'F'
[ 4] [ 1] 'E' 'K'
[ 5] [ 0] 'B' 'C'
[ 5] [ 0] 'D' 'G'
this is my cell array of five patients .i need to select the patient with status one only and display them .

채택된 답변

Star Strider
Star Strider 2015년 10월 10일
Assuming this is your cell array structure, the ‘Pts_Status_1’ array will return the correct patient information:
patients = {'patient' 'status' 'genotype' 'genotype'
[ 1] [ 1] 'A' 'C'
[ 1] [ 1] 'E' 'F'
[ 2] [ 1] 'B' 'D'
[ 2] [ 1] 'E' 'G'
[ 3] [ 0] 'D' 'G'
[ 3] [ 0] 'I' 'K'
[ 4] [ 1] 'C' 'F'
[ 4] [ 1] 'E' 'K'
[ 5] [ 0] 'B' 'C'
[ 5] [ 0] 'D' 'G'};
Pts_Status_1 = patients(cellfun(@(x)isequal(x,1), patients(:,2)), :)
Pts_Status_1 =
[1] [1] 'A' 'C'
[1] [1] 'E' 'F'
[2] [1] 'B' 'D'
[2] [1] 'E' 'G'
[4] [1] 'C' 'F'
[4] [1] 'E' 'K'
  댓글 수: 1
Star Strider
Star Strider 2015년 10월 10일
My pleasure.
The Pts_Status_1 assignment uses the cellfun function to compute with the cell array (since cell arrays can be difficult to address directly). Here, it uses the isequal function to compare the second column of your ‘patients’ array with 1, and creates a logical index vector (made up of logical true-false, or 1-0 elements), chooses only those that are ‘true’, and then outputs the entire row. Another way of coding it would be:
index = cellfun(@(x)isequal(x,1), patients(:,2));
Pts_Status_1 = patients(index, :)
I chose to do it in one line instead. Both will work, but experimenting with the two-line version will let you see how the code works.
For a full description of how the code works, see the documentation for the functions cellfun and isequal, and the documentation on ‘Anonymous Functions’ and Array Indexing, specifically ‘logical indexing’.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by