dismember or intersect

2 views (last 30 days)
Trader
Trader on 4 Apr 2012
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 Comment
Geoff
Geoff on 4 Apr 2012
By the way, typo on your definition of b. I assume (by your value for c) that you meant 'mary', not 'marry'.

Sign in to comment.

Answers (1)

Geoff
Geoff on 4 Apr 2012
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 Comments
Geoff
Geoff on 4 Apr 2012
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 on 4 Apr 2012
(or did I mean column-vectors?!) =)

Sign in to comment.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!