Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slow NaN relational comparisons in MATLAB

Tags:

matlab

It appears that doing numerical relational operations (greater than, less than) on NaNs is 10 times slower than on non-NaNs in MATLAB R2013a (ver 8.1).

>> a = rand(10000);
>> b = NaN(size(a));

>> tic; a>0; toc
Elapsed time is 0.083838 seconds.

>> tic; b>0; toc
Elapsed time is 0.991742 seconds.

Some experimentation shows the time taken scales with the proporation of NaNs in the array, such that an array of all NaNs takes longest and all non-NaNs is quickest. Infs are as quick as non-NaNs.

I'm doing comparisons on arrays with large numbers of NaNs. To hack around this slow down, I'm replacing NaNs in my arrays with Infs (e.g. -Inf if I were doing b>0). This helps, but the replacement itself is slow. Indeed it's only because I'm doing many such comparisons on the same array that the one-off replacement helps overall.

So my question is, does anyone have any better ideas for comparing against lots of NaNs?

like image 459
Justin Avatar asked Nov 25 '15 16:11

Justin


1 Answers

I'm using Matlab R2014a, and the time is the same. However, I suggest doing the following to see if it works: tic; c = isnan(b) ; toc;

This allows to transform the matrix of NaN to a matrix of logical, where "true" means it is a NaN. The new matrix will be faster than the old one, and you would simply have to redefine your comparison. For example, if you have a matrix "A" containing numbers and NaN, and you want to find numbers greater than 0, you would have:

A = myMatrix;

% The inverse sign "~" means that "true" is a number, while "false" is a nan
B = ~isnan(A);  

% Greater than 0 for non-nan
C = B & A>0
like image 140
DomDev Avatar answered Sep 20 '22 00:09

DomDev