在 matlab 函数中处理 NaN [英] Dealing with NaN's in matlab functions

查看:38
本文介绍了在 matlab 函数中处理 NaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 matlab 是否有一种内置的方法来处理函数调用中的 NaN.更具体地说,我试图取其中包含 NaN 的向量的平均值.例如,在 R

I was wondering if matlab has a built in way to deal with NaN's in function calls. More specifically, I am trying to take the mean of a vector that has a NaN in it. For example, in R

> x = c(1,2,3,4,NA)
> mean(x)
[1] NA
> mean(x,na.rm=TRUE)
[1] 2.5

在 Matlab 中,在一行中是否有类似的东西(我不想编写自己的函数,也不必在计算平均值之前循环查找 NaN ).

Is there something comprable to this in Matlab that is in one line (I don't want to write my own function nor have to loop to find NaN's before calculating the mean).

此外,我无权访问统计工具箱,因此我无法使用诸如 nanmean() 之类的东西.

Also, I do not have access to the statistics toolbox so I can't use something like nanmean().

推荐答案

您可以执行类似 mean(x(~isnan(x))) 的操作.如果你愿意,你也可以写一堆这样的包装器并将它们放在你的 startup.m 文件中.

You could do something like mean(x(~isnan(x))). If you want you could also write a bunch of wrappers like this and put them in your startup.m file.

这篇关于在 matlab 函数中处理 NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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