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};

 Accepted Answer

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.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!