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 次查看(过去 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
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Numeric Types 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!