필터 지우기
필터 지우기

dismember or intersect

조회 수: 1 (최근 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