how can i compare two string matrix with conditions ?

7 ビュー (過去 30 日間)
vengat manickam
vengat manickam 2016 年 1 月 19 日
コメント済み: Andrei Bobrov 2016 年 1 月 19 日
I want to compare two string matrix like a=[yes yes;no no] &b=[no yes;yes no] with conditions like if yes&yes=a;no&no=b;yes&no=c;no&yes=d..and i want to count no of common elements like 'no' (common in both matrix(a(2,2)&b(2,2)).
  1 件のコメント
Guillaume
Guillaume 2016 年 1 月 19 日
編集済み: Guillaume 2016 年 1 月 19 日
Please, use valid matlab syntax in your example (or accurate terms for what you describe). You cannot have a string matrix like: a=[yes yes;no no]
You could have a 2D char array (what you could call a string matrix)
a = ['yes' 'yes'; 'no ' 'no '] %note the extra spaces!
which is exactly the same as
a = ['yesyes'; 'no no '];
or you could have a 2D cell array of strings
a = {'yes' 'yes'; 'no' 'no'};
Your question about conditions is also not clear because again, you're not using syntax that makes sense.

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

採用された回答

Stephen23
Stephen23 2016 年 1 月 19 日
>> a={'yes','yes';'no','no'};
>> b={'no','yes';'yes','no'};
>> X = strcmp(cat(3,a,b),'yes');
>> Y = 1 + diff(X,1,3) + 2*all(X,3)
Y =
0 3
2 1
Where 0=(yes,no), 1=(no,no), 2=(no,yes), 3=(yes,yes).

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2016 年 1 月 19 日
a={'yes' 'yes';'no' 'no'}
b={'no' 'yes';'yes' 'no'}
aby = cellfun(@(x)strcmp(x,'yes'),a)
bby = cellfun(@(x)strcmp(x,'yes'),b)
a1 = aby & bby
b1 = ~aby & ~bby
c = aby & ~bby
d = ~aby & bby
  2 件のコメント
Stephen23
Stephen23 2016 年 1 月 19 日
編集済み: Stephen23 2016 年 1 月 19 日
Note that cellfun is not required, as strcmp also works on cell arrays:
>> strcmp(a,'yes')
ans =
1 1
0 0
Andrei Bobrov
Andrei Bobrov 2016 年 1 月 19 日
Thank you Stephen!

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

カテゴリ

Help Center および File ExchangeString Parsing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by