How do I compare two different cell arrays whose elements are NOT strings?

5 views (last 30 days)
I have two cell arrays, A and B, each of which is filled with matrices of differing values. If I want all elements in B that are not in A, how would I find that? (say A{1,1} = B{1,1} but A{2,1} ~= B{2,1}, how could I have matlab return this?) I tried using setdiff but matlab returned an error saying that setdiff only deals with cell arrays of strings.
I need to use a cell array to save these matrices as they are matrices of different sizes.

Answers (1)

Image Analyst
Image Analyst on 5 Jul 2015
You can loop over corresponding cells and use isequal to check:
if isequal(A{rowa, cola}, B{rowb, colb})
% Cell contents are equal.
else
% Cell contents are not equal.
end
Of course rowa might be the same as rowb as you seem to indicate, but it might not be, particularly if A and B don't have the same number of cells in them. It just depends on what cell of A you're comparing to what cell of B.
Now take some action depending on whether the cell contents are equal or not.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!