フィルターのクリア

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 Center および File ExchangeGenomics and Next Generation Sequencing についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by