如何使用内置函数将Inf和NaN替换为零 [英] how to replace Inf and NaN with zero using built in function

查看:817
本文介绍了如何使用内置函数将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.

这篇关于如何使用内置函数将Inf和NaN替换为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆