how to access the cell array ?
1 次查看(过去 30 天)
显示 更早的评论
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 .
0 个评论
采纳的回答
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
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 Center 和 File Exchange 中查找有关 Genomics and Next Generation Sequencing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!