为什么用k(k == NaN)= SomeNumber替换矩阵的NaN无效,其中k是要运算的矩阵 [英] Why does replacing NaNs of a matrix not work with k(k==NaN) = SomeNumber , where k is the matrix to be operated
问题描述
给出一个矩阵:-
k = [1 2 3 ;
4 5 6 ;
7 8 NaN];
如果我想用0替换数字(例如2),可以使用:k(k==2) = 0
.它可以正常工作,并给出以下预期答案:-
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
但是,如果我尝试替换NaN
,为什么这不起作用,即k(k==NaN) = 0
给出了这个信息:
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
尽管我能够使用:k(isnan(k))=0
达到期望的结果.但是为什么第一种方法不起作用?
Although I am able to achieve the desired result using: k(isnan(k))=0
. But why does the first approach not work?
推荐答案
因为NaN==NaN
是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.
是0/0 == Inf-Inf
吗?绝对不是.两者都是NaN
.
Is 0/0 == Inf-Inf
? Definetly not. Both are NaN
though.
这篇关于为什么用k(k == NaN)= SomeNumber替换矩阵的NaN无效,其中k是要运算的矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!