I have two cell arrays 'A' and 'B', each with only numeric values for only the first columns, I want to compare all elements of the first column of A with each element of the first column of B.

1 view (last 30 days)
My code is the one below, it is not doing the comparison:
for r=1:6
for c=1:2
if((A(r,1)==B(c,1)))
x(r,1)="equal";
end
end
end

Accepted Answer

Image Analyst
Image Analyst on 29 Jul 2018
Sort of close, but try using isequal() instead of ==.
Try this:
% Make up A and B because poster forgot to upload them.
% Guess, and just make them random integers.
rows = 1000;
columns = 3;
A = cell(rows, columns);
for row = 1 : rows
for col = 1 : columns
A{row, col} = randi(5, 1, 3);
B{row, col} = randi(5, 1, 3);
end
end
% Now we have A and B cell arrays and we can begin.
% Get size of A.
[rows, columns] = size(A);
% Assume B is the same size as A.
% Make cell array for strings saying if the
% corresponding cells are equal or not.
equality = cell(rows, columns);
% Compare every cell in the cell array for equality with the isequal() function.
for row = 1 : rows
for col = 1 : columns
if isequal(A(row, col), B(row, col))
% A and B are equal for this cell location.
equality{row, col} = 'equal';
else
% A and B are NOT equal for this cell location.
equality{row, col} = 'not equal';
end
end
end
% Print to command window
equality

More Answers (0)

Categories

Find more on Numeric Types 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!