Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: eliminate NaNs from vector
Date: Sun, 19 Sep 2010 17:24:04 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 50
Message-ID: <i75gvk$a6s$1@fred.mathworks.com>
References: <2f7a23e0-7b0a-408c-b6ba-0163e3cabf5e@e20g2000vbn.googlegroups.com> <i73d9v$9ve$1@fred.mathworks.com> <i73g24$1qv$1@fred.mathworks.com> <1284909569.683958@irys.nyx.net>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1284917044 10460 172.30.248.38 (19 Sep 2010 17:24:04 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sun, 19 Sep 2010 17:24:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1886545
Xref: news.mathworks.com comp.soft-sys.matlab:671276

fburton@nyx.net (Francis Burton) wrote in message <1284909569.683958@irys.nyx.net>...
> In article <i73g24$1qv$1@fred.mathworks.com>,
> Oleg Komarov <oleg.komarovRemove.this@hotmail.it> wrote:
> >> > What's the fastest way of setting all NaN values, inside a vector X,
> >> > equal to 1?
> >> - - - - - -
> >>  X(isnan(X)) = 1;
> >
> >X = min(X,max(X,1)); 
> >
> >Should be faster...
> 
> Neat solution! However, on my machine, the latter appears to be
> somewhat slower (as well as less readable).
> 
> With N=30 million elements containing mostly random numbers or
> NaNs, times in seconds were:
> 
>            isnan       minmax
> 1   NaN    0.16         0.4
> N-1 NaNs   2.1          3.0
> 
> and intermediate timings with roughly 50% NaNs.
> 
> As usual, YMMV!
> 
> Francis

Strange...

In = rand(30*10e6,1);
In(ceil(rand(20*10e6,1)*(30*10e6-1))) = NaN;
sum(isnan(In)); % 145974916 ~50%

X1 = In;
tic
X1 = min(In,max(X1,1)); 
toc

X2 = In;
tic
X2(isnan(X2)) = 1; 
toc

isequal(X1,X2) % 1

Elapsed time is 2.920996 seconds.
Elapsed time is 8.590940 seconds.

Oleg