Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does replacing NaNs of a matrix not work with k(k==NaN) = SomeNumber , where k is the matrix to be operated

Tags:

matlab

Given a matrix:-

k = [1   2   3  ;
     4   5   6  ;
     7   8  NaN];

If I want to replace a number, say 2, with 0, I can use this: k(k==2) = 0 . It works correctly and gives the following expected answer:-

k =

     1     0     3
     4     5     6
     7     8   NaN

But why does this not work if I try to replace NaN, i.e. k(k==NaN) = 0 gives this:

k =

     1     2     3
     4     5     6
     7     8   NaN

Although I am able to achieve the desired result using: k(isnan(k))=0. But why does the first approach not work?

like image 330
Sardar Usama Avatar asked Jul 26 '16 10:07

Sardar Usama


1 Answers

Because NaN==NaN is 0.

Not a number is equal to not a number? Not really, they are not numbers, but not necessarily the same thing. This is by design.

Is 0/0 == Inf-Inf ? Definetly not. Both are NaN though.

Read more here

like image 122
Ander Biguri Avatar answered Oct 15 '22 08:10

Ander Biguri