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:
How to replace NANs with numbers

Subject: How to replace NANs with numbers

From: Aaron Anstey

Date: 8 Sep, 2010 09:36:04

Message: 1 of 18

The last few columns of my data vector contain "NAN", I want to replace the "NAN"s with the number 56. Please can someone suggest a command to do this.
Thanks.

Subject: How to replace NANs with numbers

From: Oleg Komarov

Date: 8 Sep, 2010 09:44:05

Message: 2 of 18

"Aaron Anstey" <elaja2@lboro.ac.uk> wrote in message <i67le4$qnt$1@fred.mathworks.com>...
> The last few columns of my data vector contain "NAN", I want to replace the "NAN"s with the number 56. Please can someone suggest a command to do this.
> Thanks.

a hint:
help isnan

Oleg

Subject: How to replace NANs with numbers

From: Aaron Anstey

Date: 8 Sep, 2010 10:38:04

Message: 3 of 18

"Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message <i67lt5$r6c$1@fred.mathworks.com>...
> "Aaron Anstey" <elaja2@lboro.ac.uk> wrote in message <i67le4$qnt$1@fred.mathworks.com>...
> > The last few columns of my data vector contain "NAN", I want to replace the "NAN"s with the number 56. Please can someone suggest a command to do this.
> > Thanks.
>
> a hint:
> help isnan
>
> Oleg

Hello Oleg,
I have tried the isnan and that returns 1s ans 0s. I then need to replace the 1s with the figure 56 and at the same time keep all the results that are not NANs so that I end up with a full set of data (all NANs replaced by 56). This is the bit I could do with some help on please.
Thanks

Subject: How to replace NANs with numbers

From: Oleg Komarov

Date: 8 Sep, 2010 11:05:08

Message: 4 of 18

> Hello Oleg,
> I have tried the isnan and that returns 1s ans 0s. I then need to replace the 1s with the figure 56 and at the same time keep all the results that are not NANs so that I end up with a full set of data (all NANs replaced by 56). This is the bit I could do with some help on please.
> Thanks

A(isnan(A)) = 56;

I suggest you to read the first 4 chapters of the getting started guide.

Oleg

Subject: How to replace NANs with numbers

From: Sean

Date: 8 Sep, 2010 14:22:03

Message: 5 of 18

"Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message <i67ql4$a6n$1@fred.mathworks.com>...
> > Hello Oleg,
> > I have tried the isnan and that returns 1s ans 0s. I then need to replace the 1s with the figure 56 and at the same time keep all the results that are not NANs so that I end up with a full set of data (all NANs replaced by 56). This is the bit I could do with some help on please.
> > Thanks
>
> A(isnan(A)) = 56;
>
> I suggest you to read the first 4 chapters of the getting started guide.
>
> Oleg

Or, since NaNs aren't equal to each other:
A(A~=A) = 56;

Subject: How to replace NANs with numbers

From: Cris Luengo

Date: 8 Sep, 2010 14:34:08

Message: 6 of 18

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i6866b$rej$1@fred.mathworks.com>...
> "Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message <i67ql4$a6n$1@fred.mathworks.com>...
> > > Hello Oleg,
> > > I have tried the isnan and that returns 1s ans 0s. I then need to replace the 1s with the figure 56 and at the same time keep all the results that are not NANs so that I end up with a full set of data (all NANs replaced by 56). This is the bit I could do with some help on please.
> > > Thanks
> >
> > A(isnan(A)) = 56;
> >
> > I suggest you to read the first 4 chapters of the getting started guide.
> >
> > Oleg
>
> Or, since NaNs aren't equal to each other:
> A(A~=A) = 56;

Or, since NaNs aren't smaller or equal to Inf:
A(~(A<=inf)) = 56;

Subject: How to replace NANs with numbers

From: Oleg Komarov

Date: 8 Sep, 2010 15:01:22

Message: 7 of 18

"Cris Luengo" <cris.luengo@google.for.my.name.to.contact.me> wrote in message <i686t0$en2$1@fred.mathworks.com>...
> "Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i6866b$rej$1@fred.mathworks.com>...
> > "Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message <i67ql4$a6n$1@fred.mathworks.com>...
> > > > Hello Oleg,
> > > > I have tried the isnan and that returns 1s ans 0s. I then need to replace the 1s with the figure 56 and at the same time keep all the results that are not NANs so that I end up with a full set of data (all NANs replaced by 56). This is the bit I could do with some help on please.
> > > > Thanks
> > >
> > > A(isnan(A)) = 56;
> > >
> > > I suggest you to read the first 4 chapters of the getting started guide.
> > >
> > > Oleg
> >
> > Or, since NaNs aren't equal to each other:
> > A(A~=A) = 56;
>
> Or, since NaNs aren't smaller or equal to Inf:
> A(~(A<=inf)) = 56;

I suggest to use isnan since it's a more readable solution...

Oleg

Subject: How to replace NANs with numbers

From: Cris Luengo

Date: 8 Sep, 2010 16:09:05

Message: 8 of 18

> > > > A(isnan(A)) = 56;
> > > >
> > > Or, since NaNs aren't equal to each other:
> > > A(A~=A) = 56;
> >
> > Or, since NaNs aren't smaller or equal to Inf:
> > A(~(A<=inf)) = 56;
>
> I suggest to use isnan since it's a more readable solution...
>
> Oleg

Absolutely. I was aiming for the most obtuse answer... :)

Cris.

Subject: How to replace NANs with numbers

From: Steve Amphlett

Date: 8 Sep, 2010 16:50:04

Message: 9 of 18

"Cris Luengo" <cris.luengo@google.for.my.name.to.contact.me> wrote in message <i68cf1$10c$1@fred.mathworks.com>...
> > > > > A(isnan(A)) = 56;
> > > > >
> > > > Or, since NaNs aren't equal to each other:
> > > > A(A~=A) = 56;
> > >
> > > Or, since NaNs aren't smaller or equal to Inf:
> > > A(~(A<=inf)) = 56;
> >
> > I suggest to use isnan since it's a more readable solution...
> >
> > Oleg
>
> Absolutely. I was aiming for the most obtuse answer... :)
>
> Cris.

Ah, but you are supposed to speed-test the various non-intuitive forms of simple expressions to find the fastest.

Subject: How to replace NANs with numbers

From: Walter Roberson

Date: 8 Sep, 2010 16:56:14

Message: 10 of 18

On 10-09-08 11:50 AM, Steve Amphlett wrote:
> "Cris Luengo" <cris.luengo@google.for.my.name.to.contact.me> wrote in
> message <i68cf1$10c$1@fred.mathworks.com>...
>> > > > > A(isnan(A)) = 56;
>> > > > > > > > Or, since NaNs aren't equal to each other:
>> > > > A(A~=A) = 56;
>> > > > > Or, since NaNs aren't smaller or equal to Inf:
>> > > A(~(A<=inf)) = 56;
>> > > I suggest to use isnan since it's a more readable solution...
>> > > Oleg
>>
>> Absolutely. I was aiming for the most obtuse answer... :)
>>
>> Cris.
>
> Ah, but you are supposed to speed-test the various non-intuitive forms
> of simple expressions to find the fastest.

That's not how it's played: when someone hasn't been doing their research, the
goal is to come up with the _slowest_ and most opaque one that you can get
them to accept -- especially for assignments that are being marked for code
quality.

Subject: How to replace NANs with numbers

From: Matt J

Date: 8 Sep, 2010 17:00:21

Message: 11 of 18

"Steve Amphlett" <Firstname.Lastname@Where-I-Work.com> wrote in message <i68ers$c2g$1@fred.mathworks.com>...
 
> Ah, but you are supposed to speed-test the various non-intuitive forms of simple expressions to find the fastest.
==============

And I'm pretty sure that isnan would be the fastest. It has been shown in other threads that math ops on NaNs are incredibly slow.

Subject: How to replace NANs with numbers

From: Sean

Date: 8 Sep, 2010 17:03:05

Message: 12 of 18

"Steve Amphlett" <Firstname.Lastname@Where-I-Work.com> wrote in message <i68ers$c2g$1@fred.mathworks.com>...
> "Cris Luengo" <cris.luengo@google.for.my.name.to.contact.me> wrote in message <i68cf1$10c$1@fred.mathworks.com>...
> > > > > > A(isnan(A)) = 56;
> > > > > >
> > > > > Or, since NaNs aren't equal to each other:
> > > > > A(A~=A) = 56;
> > > >
> > > > Or, since NaNs aren't smaller or equal to Inf:
> > > > A(~(A<=inf)) = 56;
> > >
> > > I suggest to use isnan since it's a more readable solution...
> > >
> > > Oleg
> >
> > Absolutely. I was aiming for the most obtuse answer... :)
> >
> > Cris.
>
> Ah, but you are supposed to speed-test the various non-intuitive forms of simple expressions to find the fastest.

Oleg's way wins:
%%%%
A = rand(100);
A(randsample(10000,100)) = nan;
B = zeros(size(A));

for ii = 1:10
tic
B(isnan(A)) = 56;
toc
tic
B(A~=A) = 56;
toc
tic
B(~(A<=inf)) = 56;
toc
end
%
%Elapsed time is 0.000047 seconds.
%Elapsed time is 0.000075 seconds.
%Elapsed time is 0.000085 seconds.
%

Subject: How to replace NANs with numbers

From: Walter Roberson

Date: 8 Sep, 2010 17:14:44

Message: 13 of 18

On 10-09-08 09:34 AM, Cris Luengo wrote:
> "Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message
> <i6866b$rej$1@fred.mathworks.com>...
>> "Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message
>> <i67ql4$a6n$1@fred.mathworks.com>...
>> > > Hello Oleg,
>> > > I have tried the isnan and that returns 1s ans 0s. I then need to
>> replace the 1s with the figure 56 and at the same time keep all the
>> results that are not NANs so that I end up with a full set of data
>> (all NANs replaced by 56). This is the bit I could do with some help
>> on please.
>> > > Thanks
>> > > A(isnan(A)) = 56;
>> > > I suggest you to read the first 4 chapters of the getting started
>> guide.
>> > > Oleg
>>
>> Or, since NaNs aren't equal to each other:
>> A(A~=A) = 56;
>
> Or, since NaNs aren't smaller or equal to Inf:
> A(~(A<=inf)) = 56;

Or to do it without all those nasty logical operations:

A = min(A,max(A,56));

Subject: How to replace NANs with numbers

From: Cris Luengo

Date: 15 Sep, 2010 15:56:20

Message: 14 of 18

Walter Roberson <roberson@hushmail.com> wrote in message <i68gde$jq1$1@canopus.cc.umanitoba.ca>...
> On 10-09-08 09:34 AM, Cris Luengo wrote:
> > "Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message
> > <i6866b$rej$1@fred.mathworks.com>...
> >> "Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message
> >> <i67ql4$a6n$1@fred.mathworks.com>...
> >> > > Hello Oleg,
> >> > > I have tried the isnan and that returns 1s ans 0s. I then need to
> >> replace the 1s with the figure 56 and at the same time keep all the
> >> results that are not NANs so that I end up with a full set of data
> >> (all NANs replaced by 56). This is the bit I could do with some help
> >> on please.
> >> > > Thanks
> >> > > A(isnan(A)) = 56;
> >> > > I suggest you to read the first 4 chapters of the getting started
> >> guide.
> >> > > Oleg
> >>
> >> Or, since NaNs aren't equal to each other:
> >> A(A~=A) = 56;
> >
> > Or, since NaNs aren't smaller or equal to Inf:
> > A(~(A<=inf)) = 56;
>
> Or to do it without all those nasty logical operations:
>
> A = min(A,max(A,56));

That one is very clever. And very fast! We've got a new winner! :)

>> nantimetest
ans =
   5.9539e-05
ans =
   8.9543e-05
ans =
   1.0356e-04
ans =
   5.6491e-05


function nantimetest
A = rand(100);
A(randsample(10000,100)) = nan;
timeit(@()method1(A))
timeit(@()method2(A))
timeit(@()method3(A))
timeit(@()method4(A))

function A = method1(A)
A(isnan(A)) = 56;

function A = method2(A)
A(A~=A) = 56;

function A = method3(A)
A(~(A<=inf)) = 56;

function A = method4(A)
A = min(A,max(A,56));

Subject: How to replace NANs with numbers

From: Walter Roberson

Date: 15 Sep, 2010 16:06:04

Message: 15 of 18

On 15/09/10 10:56 AM, Cris Luengo wrote:
> Walter Roberson <roberson@hushmail.com> wrote in message

>> Or to do it without all those nasty logical operations:
>> A = min(A,max(A,56));

> That one is very clever. And very fast! We've got a new winner! :)

Sigh, it was supposed to be slow and snarky, an example of bad code....

Subject: How to replace NANs with numbers

From: Cris Luengo

Date: 15 Sep, 2010 16:14:20

Message: 16 of 18

Walter Roberson <roberson@hushmail.com> wrote in message <N96ko.2851$Bg.1224@newsfe22.iad>...
> On 15/09/10 10:56 AM, Cris Luengo wrote:
> > Walter Roberson <roberson@hushmail.com> wrote in message
>
> >> Or to do it without all those nasty logical operations:
> >> A = min(A,max(A,56));
>
> > That one is very clever. And very fast! We've got a new winner! :)
>
> Sigh, it was supposed to be slow and snarky, an example of bad code....

Hehe.

I think it's faster because you don't set up an index array. Avoids all that nasty indexing. A true example of good coding! ;)

Cris.

Subject: How to replace NANs with numbers

From: Oleg Komarov

Date: 15 Sep, 2010 16:15:22

Message: 17 of 18

Walter Roberson <roberson@hushmail.com> wrote in message <N96ko.2851$Bg.1224@newsfe22.iad>...
> On 15/09/10 10:56 AM, Cris Luengo wrote:
> > Walter Roberson <roberson@hushmail.com> wrote in message
>
> >> Or to do it without all those nasty logical operations:
> >> A = min(A,max(A,56));
>
> > That one is very clever. And very fast! We've got a new winner! :)
>
> Sigh, it was supposed to be slow and snarky, an example of bad code....

LOL!!!

I was thinking all this time how many years would it take me to understand that your line of code was better than a logical operation....and why.

Not that I got an answer!

Oleg

Subject: How to replace NANs with numbers

From: Walter Roberson

Date: 15 Sep, 2010 17:45:04

Message: 18 of 18

On 15/09/10 11:15 AM, Oleg Komarov wrote:
> Walter Roberson <roberson@hushmail.com> wrote in message

>> >> A = min(A,max(A,56));

> I was thinking all this time how many years would it take me to
> understand that your line of code was better than a logical
> operation....and why.

The operations can be done completely in-place in the above line, in
theory, with no intermediate vector _necessarily_ constructed, if the
JIT is smart enough. I would, however, not expect the JIT to be that smart.

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