Filter löschen
Filter löschen

dismember or intersect

1 Ansicht (letzte 30 Tage)
Trader
Trader am 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 Kommentar
Geoff
Geoff am 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'.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by