compare two matrices with isequal

6 次查看(过去 30 天)
Hi. I need to compare matrices present inside two separate cells (cells 'AAA' and 'BBB').
AAA = importdata("AAA.mat");
BBB = importdata("BBB.mat");
To be precise AAA{1,1} must equal BBB{1,1}, AAA{2,1} must equal BBB{2,1} and so on...
Is it correct to use the isequal command in this way?
AAA = importdata("AAA.mat");
BBB = importdata("BBB.mat");
equal = isequal(AAA,BBB);
Or should I compare the matrices inside a loop as in this case?
AAA = importdata("AAA.mat");
BBB = importdata("BBB.mat");
% FIRST COLUMN
check_1 = {};
check_2 = {};
for K_1_col = 1:height(AAA)
AAA_1_col = AAA{K_1_col,1};
BBB_1_col = BBB{K_1_col,1};
equal_1_col = isequal(AAA_1_col,BBB_1_col);
result = AAA_1_col==BBB_1_col;
check_1 = [check_1;{equal_1_col}];
check_2 = [check_2;{result}];
end
Or are all three ways equivalent ?

采纳的回答

Bruno Luong
Bruno Luong 2023-7-25
编辑:Bruno Luong 2023-7-25
A single call of isequal on the top level is OK.
Note that is you have NaN somewhere in your data, isequal will fail to detect indentical arrays.
a=rand(2);
c=repmat({a},3,4);
d=c;
isequal(c,d) % true
ans = logical
1
a(1)=nan;
c=repmat({a},3,4);
d=c;
isequal(c,d) % false
ans = logical
0
  5 个评论
Bruno Luong
Bruno Luong 2023-7-25
编辑:Bruno Luong 2023-7-25
OK, thank you. But using isequal command directly are all matrices analyzed ?
Yes. That's what I said everytime you ask the same question.
If two matrices placed in the same position are not equal how can I identify that matrix?
You can't. ISEQUAL only gives yes/no answer.
Steven Lord
Steven Lord 2023-7-25
If two matrices placed in the same position are not equal how can I identify that matrix?
You can't. ISEQUAL only gives yes/no answer.
You can't with isequal. The isequal function returns whether or not the whole array is the same. For checking individual elements you'll need to use == but that's not defined for cell arrays (since a cell array can contain anything.)
For this purpose you could use a for loop over the elements of AA and BB (though if you have nested cell arrays, you'd need to dig down into the data nested inside) or you could use cellfun to do it in one function call. The comparisonFunction compares all the elements in its two inputs to determine whether they are equal then makes sure all the elements of the resulting array (operating in 'all' the dimensions of that result) are true. If it is, comparisonFunction returns true, otherwise it returns false.
AA = {1, [2, 3]; [4; 5], [6 7; 8 9]};
BB = {0, [2, 3]; [4; 5], [6 7; 9 8]};
comparisonFunction = @(x, y) all(x == y, 'all');
result = cellfun(comparisonFunction, AA, BB)
result = 2×2 logical array
0 1 1 0
In this case, to handle the scenario where comparisonFunction throws an error (if the contents of the cells are different sizes, for example) I'd specify an 'ErrorHandler' that just returns false (if the inputs aren't the same size, they can't be equal.)
CC = BB;
CC{2, 2} = magic(3);
result = cellfun(comparisonFunction, AA, CC, 'ErrorHandler', @(varargin) false)
result = 2×2 logical array
0 1 1 0
What if I hadn't specified the ErrorHandler? Well, == can't compare a 2-by-2 (the 4th element in AA) with a 3-by-3 (the 4th element in CC) because ...
result = cellfun(comparisonFunction, AA, CC)
Arrays have incompatible sizes for this operation.

Error in solution>@(x,y)all(x==y,'all') (line 3)
comparisonFunction = @(x, y) all(x == y, 'all');

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by