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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With