how to access the cell array ?

1 Ansicht (letzte 30 Tage)
joha
joha am 10 Okt. 2015
Kommentiert: Star Strider am 10 Okt. 2015
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 .

Akzeptierte Antwort

Star Strider
Star Strider am 10 Okt. 2015
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 Kommentar
Star Strider
Star Strider am 10 Okt. 2015
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’.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Genomics and Next Generation Sequencing finden Sie in Help Center und File Exchange

Tags

Noch keine Tags eingegeben.

Community Treasure Hunt

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

Start Hunting!

Translated by