1

I am trying to compare two .mat files using the Matlab Comparison Tool. In the comparison result the NaN values are highlighted meaning they are different (even though they are not). How can one handle the NaN values when using the Matlab Comparison Tool.

enter image description here

cloudviz
  • 971
  • 4
  • 15
  • 40
  • 1
    If all you want to do is know whether contents of two mat-files are equal, `a=load('file1.mat'); b=load(`file2.mat`); bothAreSame = isequaln(a,b)` will do the trick. – Jonas Dec 24 '13 at 22:46

2 Answers2

4

Try isnan.

   label = false(size(data)); 
   different =  true;
   label(isnan(data)) = different

You can also use eq:

b=[1 2;nan 4];
 a=[1 2;nan 4];
 eq(a,b)

 >>  1     1
     0     1
lennon310
  • 12,503
  • 11
  • 43
  • 61
  • That would mean I'll have to identify the data that is affected and label them. I thought there would be an easier way from the Matlab comparison tool. – cloudviz Dec 24 '13 at 14:26
4

NaN is by definition "not a number". Quoting the documentation,

A NaN is obtained as a result of mathematically undefined operations like 0.0/0.0 and inf-inf.

Therefore, mathematically speaking, equality is not even defined between NaN's. For example; trying to compute 0/0 gives NaN, as does 0/0+1. Would you say 0/0 and 0/0+1 are equal? Would you say they are different? Neither: 0/0 and 0/0+1 simply don't exist.

Programmatically, on the other hand, NaN is a well defined "value", as is equality between NaN's: namely, NaN is always unequal to NaN. The rationale of this behaviour is probably the fact that mathematically NaN is undefined, and so it can't be equal to itself.

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • 2
    Sorry to nit-pick, but equality *is* defined between NaN's: the definition is that NaN is not equal to NaN. The expression `nan == nan` has a perfectly well-defined value, which is `false`. – Sam Roberts Jan 02 '14 at 15:05
  • 1
    @SamRoberts You're right. In Matlab it _is_ defined, although mathematically it would be undefined because the objects it applies to are non-existent. I have updated my answer – Luis Mendo Jan 02 '14 at 17:23
  • 1
    I'm sorry, I'm going to have to nit-pick again, I can't help myself. It's not MATLAB (or MathWorks) that does the defining, it's the IEEE standards for floating point arithmetic. Since this document is authored by mathematicians and is a consensus among them, equality between NaNs is *mathematically* defined. – Sam Roberts Jan 02 '14 at 19:10