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.

조회 수: 6 (최근 30일)
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

채택된 답변

Image Analyst
Image Analyst 2018년 7월 29일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by