how to replace Inf and NaN with zero using built in function

放肆的年华 提交于 2019-12-30 05:55:27

问题


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

For example

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]

回答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

does the trick, this uses logical indexing

~ 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.




回答2:


Similarly for NaN, you can use isnan() to replace these elements with whatever you want.



来源:https://stackoverflow.com/questions/10485294/how-to-replace-inf-and-nan-with-zero-using-built-in-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!