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.
If the
nan
density is high enough, then a double loop will be the fastest method. This is because the search of a row can be discarded as soon as the firstnan
is found. For example, consider the following speed test:The results are:
Of course, the
nan
density is so high in this simulation that none of the rows arenan
free. But you never said anything about thenan
density of your matrix, so I figured I'd post this answer for general consumption and contemplation :-)Can you tell more about what you want to do with the indices
for 100 times in a loop, 0.1404 sec
Edit: 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:
Out of the two methods you suggested (denoted
f
andg
) in the question the first is faster (usingtimeit
):any()
is faster thanall()
orsum()
. try:correction: it seems that
sum()
approach is faster: