check if the input is the same in the cell arrays a = {a,b,c,d} b = {c,d,a,b} in matlab

To check if two cell arrays have the same elements we can use the isequal function. However, if the elements are not ordered in the same way, isequal function will return false.

In order to check if both cell arrays have the same elements regardless of their order we can use the sort function.

Here is an example code:

main.m
a = {'a', 'b', 'c', 'd'};
b = {'c', 'd', 'a', 'b'};

if isequal(sort(a), sort(b))
    disp('The cell arrays have the same elements');
else
    disp('The cell arrays do not have the same elements');
end
202 chars
9 lines

In this example, we are using sort function to sort both a and b cell arrays in ascending order. Then, we are using the isequal function to check if they have the same elements. Since both cell arrays have the same elements, regardless of order, this code will output 'The cell arrays have the same elements'.

related categories

gistlibby LogSnag