Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
eliminate NaNs from vector

Subject: eliminate NaNs from vector

From: Mauro

Date: 18 Sep, 2010 21:29:20

Message: 1 of 5

What's the fastest way of setting all NaN values, inside a vector X,
equal to 1?

Subject: eliminate NaNs from vector

From: Roger Stafford

Date: 18 Sep, 2010 22:09:03

Message: 2 of 5

Mauro <msmscarlatti@googlemail.com> wrote in message <2f7a23e0-7b0a-408c-b6ba-0163e3cabf5e@e20g2000vbn.googlegroups.com>...
> What's the fastest way of setting all NaN values, inside a vector X,
> equal to 1?
- - - - - -
 X(isnan(X)) = 1;

Roger Stafford

Subject: eliminate NaNs from vector

From: Oleg Komarov

Date: 18 Sep, 2010 22:56:05

Message: 3 of 5

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i73d9v$9ve$1@fred.mathworks.com>...
> Mauro <msmscarlatti@googlemail.com> wrote in message <2f7a23e0-7b0a-408c-b6ba-0163e3cabf5e@e20g2000vbn.googlegroups.com>...
> > What's the fastest way of setting all NaN values, inside a vector X,
> > equal to 1?
> - - - - - -
> X(isnan(X)) = 1;
>
> Roger Stafford

X = min(X,max(X,1));

Should be faster...

Oleg

Subject: eliminate NaNs from vector

From: fburton@nyx.net (Francis Burton)

Date: 19 Sep, 2010 15:19:29

Message: 4 of 5

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

Subject: eliminate NaNs from vector

From: Oleg Komarov

Date: 19 Sep, 2010 17:24:04

Message: 5 of 5

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

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us