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.
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
wisam qadir
el 29 de Jul. de 2018
Respondida: Image Analyst
el 29 de Jul. de 2018
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
1 comentario
Respuesta aceptada
Image Analyst
el 29 de Jul. de 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
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Numeric Types en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!