Large Matrix - Replacing NaN with zeros

  • Follow


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!
0
Reply Jon 3/24/2010 3:49:05 PM

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.
0
Reply Walter 3/24/2010 4:12:20 PM


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.
0
Reply Doug 3/24/2010 4:33:22 PM

2 Replies
608 Views

(page loaded in 0.017 seconds)

Similiar Articles:













7/22/2012 5:19:16 AM


Reply: