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

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

问题描述

我想知道matlab是否内置了处理函数调用中的 NaN 的函数。更具体地说,我试图采用其中包含 NaN 的向量的均值。例如,在R

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

可以在一行中用Matlab进行编译(我不想编写自己的函数,也不必在计算平均值之前循环找到 NaN 的)。

另外,我没有权限访问统计工具箱,所以我不能使用类似于 nanmean()

解决方案

您可以像 mean(x(〜isnan(x))) code>。如果你想要的话,你也可以写一堆这样的包装,并把它们放在你的startup.m文件中。


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

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

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

解决方案

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天全站免登陆