Reputation: 123
My goal is to compare two matrices: A
and B
in two different files:
function [Result]=test()
A_Mat= load('fileA', 'A')
B_Mat= load('fileB', 'B')
Result= A_Mat == B_Mat
end
The result that I want is a matrix that includes the difference between A
and B
.
The error that I have is:
error: binary operator '==' not implemented for 'scalar struct' by 'scalar struct' operations
Upvotes: 1
Views: 3016
Reputation: 13520
First, the operator ==
does work on matrices, and it returns a logical matrix of true/false (1/0) where the corresponding items are equal or different respectively. From the error you got it seems that you didn't read matrices from the file, but structs, and indeed, ==
doesn't work for structs.
You can use isequal
for both structs and matrices. This function returns only one value - 1 or 0 (true/false).
ADDED
After seeing @dasdingonesin answer, who actually pointed to the exact problem, I just wanted to add that when you write
A_Mat= load('fileA', 'A')
it returns a struct as with the field A
.
So:
A_Mat = s.A
Upvotes: 0
Reputation: 18207
If you simply want the difference between A
and B
you should first use load
as dasdingonesin suggested, and either check for full matrix equality with isequal
or elementwise equality with ==
. The difference, however, is simply given by -
of course:
isequal(A,B); % returns a boolean for full matrix equality
A==B; % returns a logical matrix with element wise equality
A-B; % returns a matrix with differences between the two matrices
Do note that isequal
can deal with matrices of unequal size (it will simply return 0
), whilst both ==
and -
will crash with the error Matrix dimensions must agree
.
Upvotes: 0
Reputation: 1358
The load
function doesn't return what you think it returns. Reading the extensive and easily comprehensible MATLAB documentation always helps.
function Result=test()
load('fileA', 'A');
load('fileB', 'B');
Result = A == B
end
Upvotes: 6