Converting Cell Array Output to Logical Matrix

6 ビュー (過去 30 日間)
eugene479
eugene479 2016 年 1 月 19 日
コメント済み: Star Strider 2016 年 1 月 20 日
I have an array output consisting of the following characters: "A1a", "A1b", "A2a", "A2b" for e.g.:
'A1a' 'A2a'
'A1a' 'A2b'
'A1b' 'A2a'
'A1b' 'A2b'
How do I convert it to a logical matrix such that, if the output contains the character "A1a", under the column "A1a", the value will be true" i.e. "1" ?
%Output
Row A1a A1b A2a A2b
1 1 0 1 0
2 1 0 0 1
3 0 1 1 0
4 0 1 0 1

採用された回答

Star Strider
Star Strider 2016 年 1 月 20 日
編集済み: Star Strider 2016 年 1 月 20 日
This works:
C = {'A1a' 'A2a'
'A1a' 'A2b'
'A1b' 'A2a'
'A1b' 'A2b'};
Cu = unique(C,'stable');
for k1 = 1:length(Cu)
L{k1} = cellfun(@(x) strcmpi(x,Cu(k1)), C);
N(:,:,k1) = L{k1};
end
T = squeeze(sum(N,2));
fprintf(1,'\n\tRow\t%3s\t%3s\t%3s\t%3s\n', Cu{:})
fprintf(1, '\t%2.0f\t%2.0f\t%2.0f\t%2.0f\t%2.0f\n', [[1:4]' T]')
Row A1a A1b A2a A2b
1 1 0 1 0
2 1 0 0 1
3 0 1 1 0
4 0 1 0 1
  2 件のコメント
eugene479
eugene479 2016 年 1 月 20 日
Thank you so much! It works great!
Star Strider
Star Strider 2016 年 1 月 20 日
My pleasure!

サインインしてコメントする。

その他の回答 (1 件)

Harsheel
Harsheel 2016 年 1 月 19 日
編集済み: Harsheel 2016 年 1 月 19 日
out = cellfun(@(x)(strcmp(x,'A1a')), A); % A is the input cell array

カテゴリ

Help Center および File ExchangeData Types についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by