compare the content of two cell arrays if they are the same if not show me the difference in matlab

We can use the isequal function to compare two cell arrays. If they are equal, then it would return a logical true value. If they are not equal, we can use setxor function to get the differences between the two arrays.

Here is the code snippet for the same:

main.m
% Defining Two Cell Arrays
A = {'apple', 'banana', 'orange', 'kiwi', 'grape'};
B = {'apple', 'banana', 'orange', 'grape', 'pineapple'};

% Comparing if Both are Equal
if isequal(A, B)
   disp('Both the cell arrays are the same.');
else
   % Returns the differences between the two arrays
   diff = setxor(A,B);
   disp('The cell arrays are different.');
   disp('The differences between the two arrays are:');
   disp(diff); 
end
430 chars
15 lines

The output of the code snippet would be:

main.m
The cell arrays are different.
The differences between the two arrays are:
'kiwi'
'pineapple'
94 chars
5 lines

Here, we have used a simple if loop to check if the two arrays are equal. If not, we use setxor function which will return the differences between the two arrays.

related categories

gistlibby LogSnag