compare the content of two cell arrays if they are the same in matlab

To check if the content of two cell arrays are the same, you can use the isequal function.

Here's an example:

main.m
A = {'apple', 'banana', 'cherry'};
B = {'banana', 'apple', 'cherry'};

if isequal(A,B)
    disp('The content of A and B are the same');
else
    disp('The content of A and B are not the same');
end
198 chars
9 lines

Output:

main.m
The content of A and B are not the same
40 chars
2 lines

Note that the order of the elements matters when using isequal. If you want to check if the content is the same regardless of the order, you can use sort to sort the elements before comparing:

main.m
A = {'apple', 'banana', 'cherry'};
B = {'banana', 'apple', 'cherry'};

if isequal(sort(A),sort(B))
    disp('The content of A and B are the same');
else
    disp('The content of A and B are not the same');
end
210 chars
9 lines

Output:

main.m
The content of A and B are the same
36 chars
2 lines

related categories

gistlibby LogSnag