non-looping way to compare cellstr arrays of different sizes
Show older comments
Suppose A = {'aa', 'kk', 'ccc'}, B = {'aa', 'bb', 'cc', 'dd', 'ee'}.
Is there a not-explicitly-looping way to return a logical array the same size as A that is 1 where an element of A is in B, 0 where it is not, e.g.,
"whereMember"(A,B) => [1 0 0]?
Thanks!
Accepted Answer
More Answers (1)
David Goldsmith
on 24 Aug 2011
7 Comments
Jan
on 24 Aug 2011
Small suggestions: Using "numel(A)" instead of "length(A)" considers cell arrays of any dimensions.
"la=false(size(A))" replies a logical array as ISMEMBER. Then the loop can be:
for i=1:numel(A), la(i)= any(strcmpi(A{i}, B)); end
BTW.: Your loop is faster than ISMEMBER.
Jan
on 24 Aug 2011
What is the meaning of your otehr thread: http://www.mathworks.com/matlabcentral/answers/13906-indexing-way-to-look-for-specifc-cell-arrays-in-a-cell-array-of-cell-arrays ???
David Goldsmith
on 24 Aug 2011
David Goldsmith
on 24 Aug 2011
Oleg Komarov
on 25 Aug 2011
Unfortunately yes.
David Goldsmith
on 25 Aug 2011
Jan
on 25 Aug 2011
@David: I've tried to let leo.org tell me, what affiliation is. Leo meant, that either somebody adopts me, or that it concerns my fatherhood. :-) If this means my company: I'm a freelancer. Perhaps my FEX author ID 15233 is helpful?
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!