From: Jon Smith on 24 Mar 2010 11:49 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 24 Mar 2010 12:12 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 24 Mar 2010 12:33 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.
|
Pages: 1 Prev: Fit curves to two data sets Next: Exclude selected lines of code from publishing |