dismember or intersect

4 ビュー (過去 30 日間)
Trader
Trader 2012 年 4 月 4 日
I have 2 cell arrays that have both double and characters in it. For plotting purposes, I'd like to create a 3rd array that is the size of the first array but only has the values of the second. Example:
a = {1 3 'tom';2 4 'dave'; 4 6 'mary'; 7 8 'daryl'}
b = {1 3 'tom'; 4 6 'marry'}
c = nan(size(a));
what can I do to get c to look like:
1 3 'tom'
nan nan nan
4 6 'mary'
nan nan nan
I know with an array of all numbers I could do something like: a = cell2mat(a); b = cell2mat(b); c = nan(size(a)); is = dismember(a, b); z(is) = a(is);
how can I do the same thing with strings in the array?
Thanks for your help
  1 件のコメント
Geoff
Geoff 2012 年 4 月 4 日
By the way, typo on your definition of b. I assume (by your value for c) that you meant 'mary', not 'marry'.

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

回答 (1 件)

Geoff
Geoff 2012 年 4 月 4 日
You can still use ismember on the 3rd column:
memb3 = ismember(a(:,3),b(:,3));
And a dirty little number on the other two:
memb12 = all(ismember(cell2mat(a(:,1:2)), cell2mat(b(:,1:2))),2);
Then:
is = memb12 & memb3;
  3 件のコメント
Geoff
Geoff 2012 年 4 月 4 日
Did you use my code exactly as written? The '2' in the call to 'all' is important. You should end up with memb12 and memb3 being both row-vectors. Then to get 'c' you do:
c = a(is,:);
Geoff
Geoff 2012 年 4 月 4 日
(or did I mean column-vectors?!) =)

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

カテゴリ

Help Center および File ExchangePhased Array Design and Analysis についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by