查找数组中的所有 NaN 元素

发布于 2024-08-11 07:16:12 字数 60 浏览 6 评论 0原文

MATLAB 中是否有命令允许我查找数组中的所有 NaN(非数字)元素?

Is there a command in MATLAB that allows me to find all NaN (Not-a-Number) elements inside an array?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何以笙箫默 2024-08-18 07:16:13

如前所述,最好的答案是 isnan() (尽管 woodchips 的元答案为 +1)。一个更完整的示例,说明如何将其与逻辑索引一起使用:

>> a = [1 nan;nan 2]

a =

  1   NaN
NaN     2

>> %replace nan's with 0's
>> a(isnan(a))=0

a =

 1     0
 0     2

isnan(a) 返回一个逻辑数组,一个包含 true & 的数组。 false 与 a 大小相同,“true”每个地方都有一个 nan,可用于 索引到a。

As noted, the best answer is isnan() (though +1 for woodchips' meta-answer). A more complete example of how to use it with logical indexing:

>> a = [1 nan;nan 2]

a =

  1   NaN
NaN     2

>> %replace nan's with 0's
>> a(isnan(a))=0

a =

 1     0
 0     2

isnan(a) returns a logical array, an array of true & false the same size as a, with "true" every place there is a nan, which can be used to index into a.

李白 2024-08-18 07:16:13

虽然 isnan 是正确的解决方案,但我只是指出找到它的方法。使用查找。当您不知道 MATLAB 中的函数名称时,请尝试查找。

lookfor nan

将快速为您提供一些与 NaN 一起使用的函数的名称,并为您提供其帮助块的第一行。在这里,它会列出(除其他外)

ISNAN True for Not-a-Number。

这显然是您想要使用的功能。

While isnan is the correct solution, I'll just point out the way to have found it. Use lookfor. When you don't know the name of a function in MATLAB, try lookfor.

lookfor nan

will quickly give you the names of some functions that work with NaNs, as well as giving you the first line of their help blocks. Here, it would have listed (among other things)

ISNAN True for Not-a-Number.

which is clearly the function you want to use.

尸血腥色 2024-08-18 07:16:13

我刚刚找到答案:

k=find(isnan(yourarray))

k 将是 NaN 元素索引的列表。

I just found the answer:

k=find(isnan(yourarray))

k will be a list of NaN element indicies.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文