Clear Filters
Clear Filters

same matrix but not equal problem

4 views (last 30 days)
I have two matrix: C and C1, they are totally the same, but when I used
isequal(C,C1)
Matlab returns 0.
could anyone help me figure why this happens?
Best! Yu

Accepted Answer

John D'Errico
John D'Errico on 7 Jun 2018
Edited: John D'Errico on 7 Jun 2018
When you see something that you do not understand, LOOK AT YOUR DATA.
C(1,:)
ans =
Columns 1 through 31
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
Columns 32 through 45
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
C1(1,:)
ans =
Columns 1 through 31
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
Columns 32 through 45
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
isequal(NaN,NaN)
ans =
logical
0
NaN == NaN
ans =
logical
0
NaNs are not equal to NaNs.
nnz(isnan(C))
ans =
543150
numel(C)
ans =
995220
Roughly half of your array elements are NaN.
You can test for equality, where NaNs are considered to be equal to NaNs, using isequaln.
isequaln(C,C1)
ans =
logical
1

More Answers (1)

Walter Roberson
Walter Roberson on 7 Jun 2018
The matrices contain NaN. It is a property of NaN that NaN ~= NaN
>> isequaln(C,C1)
ans =
logical
1
(requires R2012a or later)

Tags

Community Treasure Hunt

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

Start Hunting!