non-looping way to compare cellstr arrays of different sizes

1 visualización (últimos 30 días)
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!

Respuesta aceptada

Oleg Komarov
Oleg Komarov el 24 de Ag. de 2011
A = {'aa', 'kk', 'ccc'};
B = {'aa', 'bb', 'cc', 'dd', 'ee'};
ismember(A,B)
  4 comentarios
David Goldsmith
David Goldsmith el 24 de Ag. de 2011
DOH! again. (But shouldn't that be ismember(lower(A),lower(B)) - I want it to be "commutatively" case insensitive.) :-)
Thanks one more time!
Oleg Komarov
Oleg Komarov el 24 de Ag. de 2011
Yes, then lower() both.

Iniciar sesión para comentar.

Más respuestas (1)

David Goldsmith
David Goldsmith el 24 de Ag. de 2011
Here's a little function that does if the answer to the revised question is "no":
function la = cellstrIsMember(A, B, i)
% la = 1 where the elements of cellstr array A are in cellstr array B, 0 where they are not
% i is an optional parameter: if case matters, i=1 (the default),
% 0 for case-insensitive matching (which is slower)
%
% Author: David Goldsmith, Wash. State Dept. of Ecology, dgol461@ecy.wa.gov
% Release date: 2011-08-24
if nargin < 3
i = 1;
end
if i || isempty(A) || isempty(B)
la = ismember(A, B);
else
la = zeros(size(A));
for i=1:length(A)
if any(strcmpi(A{i}, B))
la(i) = 1;
end
end
end
end
  7 comentarios
David Goldsmith
David Goldsmith el 25 de Ag. de 2011
Jan: may I credit you in my by-line comment? If yes, what is your affiliation, if any?
Jan
Jan el 25 de Ag. de 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?

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by