From: Jon Smith on
Does anyone know a fast way to replace elements equal to NaN with zeros for a very large matrix. I have tried X = (~isfinite(X)) = 0, but it take forever.

Thanks!
From: Walter Roberson on
Jon Smith wrote:
> Does anyone know a fast way to replace elements equal to NaN with zeros
> for a very large matrix. I have tried X = (~isfinite(X)) = 0, but it
> take forever.

X(isnan(X)) = 0;

should be slightly faster (in theory) as it would not have to do the
extra logical operator. For faster than that, you will probably have to
go to a mex routine.
From: Doug Schwarz on
Walter Roberson wrote:
> Jon Smith wrote:
>> Does anyone know a fast way to replace elements equal to NaN with
>> zeros for a very large matrix. I have tried X = (~isfinite(X)) = 0,
>> but it take forever.
>
> X(isnan(X)) = 0;
>
> should be slightly faster (in theory) as it would not have to do the
> extra logical operator. For faster than that, you will probably have to
> go to a mex routine.

I agree with the isnan suggestion, but I seem to recall that

X(X~=X) = 0;

is even faster. It exploits the fact that NaN~=NaN returns true.

Give it a try with your matrix and see which is faster. If it's still
slow you might be running into memory problems by having to create a
temporary logical matrix that is the same size as X. If that's the case
you might want to do the replacement in blocks (e.g., column-by-column
or row-by-row).

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.