在Matlab中查找没有NaN的行的最快方法 [英] Fastest way to find rows without NaNs in Matlab

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

问题描述

我想以最快的方式找到没有任何NaN的行的索引,因为我需要做数千次.到目前为止,我已经尝试了以下两种方法:

I would like to find the indexes of rows without any NaN in the fastest way possible since I need to do it thousands of times. So far I have tried the following two approaches:

find(~isnan(sum(data, 2)));
find(all(~isnan(data), 2));

是否有聪明的方法可以加快速度?这是最好的方法吗?数据矩阵的尺寸通常为数千乘数百.

Is there a clever way to speed this up or is this the best possible? The dimension of the data matrix is usually thousands by hundreds.

推荐答案

矩阵乘法的运算速度可能比求和运算的速度快,因此,对于500 x500元素以上的矩阵(在我的Matlab 2012a计算机中),运算几乎快了两倍.所以我的解决方案是:

matrix multiplication can be faster than sum, so the operation is almost twice faster for matrices above 500 x500 elements (in my Matlab 2012a machine). So my solution is:

find(~isnan(data*zeros(size(data,2),1)))


在问题中建议的两种方法(分别表示为fg)中,第一种更快(使用 timeit ):


Out of the two methods you suggested (denoted f and g) in the question the first is faster (using timeit):

data=rand(4000);
nani=randi(numel(data),1,500);
data(nani)=NaN;
f= @() find(~isnan(sum(data, 2)));
g= @() find(all(~isnan(data), 2));
h= @() find(~isnan(data*zeros(size(data,2),1)));

timeit(f) 
ans =
     0.0263

timeit(g)
ans =
     0.1489

timeit(h)
ans =
     0.0146

这篇关于在Matlab中查找没有NaN的行的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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