如何使用内置函数将Inf和NaN替换为零

问题描述:

在八度音阶中,有一个内置函数可以将向量中的Inf/NaN替换为0

In octave, is there a build in function for replacing Inf/NaN to 0 in a vector

例如

a = log10([30 40 0 60]) => [1.4771 1.6021 -Inf 1.7782]

我可以使用有限或查找功能查找有效值的索引/位置 但是我不知道如何在不编写函数的情况下正确地复制值.

I can use finite or find function to find the index/position of the valid values but I don't know how to copy the values correctly without writing a function.

finite(a) => [1 1 0 1]

>> a = log10([30 40 0 60])
a =
      1.477    1.602    -Inf    1.778

>> a(~isfinite(a))=0
a =
      1.477    1.602    0       1.778

做到了,这使用了逻辑索引

~是布尔值/逻辑值的NOT运算符,并且isfinite(a)生成逻辑向量,其大小与a:

~ is the NOT operator for boolean/logical values and isfinite(a) generates a logical vector, same size as a:

>> ~isfinite(a)
ans =
     0     0     1     0

如您所见,这用于逻辑索引.

As you can see, this is used for the logical indexing.