Comparing cell arrays of strings of unequal size/replacing missing values with zeroes

I have two cell arrays of strings of unequal size:
A = {'dog','cat'};
B = {'dog ','cat','fish ','horse'};
How can I compare A with B string-by-string and put a zero (0) where strings in B are not in A?
I would expect a result kind of that:
B = {'dog ','cat', 0, 0};

 Respuesta aceptada

James Tursa
James Tursa el 6 de Mzo. de 2015
Editada: James Tursa el 6 de Mzo. de 2015
A = {'dog','cat'};
B = {'dog','cat','fish','horse'};
B(~ismember(B,A)) = {0}
B =
'dog' 'cat' [0] [0]
Note: Your B{1} and B{3} as shown above have an extra space at the end. I assumed this was a typo and removed it. If it is not a typo then the solution above will have to be modified.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by