dismember or intersect

2 次查看(过去 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 CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by