Matlab Comparison Tool NaN values

604 Views Asked by At

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

2

There are 2 best solutions below

3
On

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.

1
On

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