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:
find zeros and replace with NaN

Subject: find zeros and replace with NaN

From: jonathan

Date: 4 Apr, 2011 16:27:04

Message: 1 of 10

I have this matrix and I want to find all the zeros and change them to NaNs.
Example:
A=[1 0 4;3 2 0]
Should turn into
A=[1 NaN 4;3 2 NaN]

can you tell me how

Thank you for reply

Subject: find zeros and replace with NaN

From: Husam Aldahiyat

Date: 4 Apr, 2011 16:32:04

Message: 2 of 10

"jonathan " <senator314159@hotmail.com> wrote in message <incrgo$imf$1@fred.mathworks.com>...
> I have this matrix and I want to find all the zeros and change them to NaNs.
> Example:
> A=[1 0 4;3 2 0]
> Should turn into
> A=[1 NaN 4;3 2 NaN]
>
> can you tell me how
>
> Thank you for reply

>> A(~A) = nan

Subject: find zeros and replace with NaN

From: Florin Neacsu

Date: 4 Apr, 2011 16:49:05

Message: 3 of 10

"jonathan " <senator314159@hotmail.com> wrote in message <incrgo$imf$1@fred.mathworks.com>...
> I have this matrix and I want to find all the zeros and change them to NaNs.
> Example:
> A=[1 0 4;3 2 0]
> Should turn into
> A=[1 NaN 4;3 2 NaN]
>
> can you tell me how
>
> Thank you for reply

Hi Jonathan,

I saw from you profile that you want to learn Matlab, so I am taking the liberty to go with you over your problem in more detail.


You have a matrix and you are looking to change some specific values. You need to FIND those values first, right ? So,
> doc find

Find will provide you with the indices of nonzeros elements of A.

>find(A)

ans =

     1
     2
     4
     5

(N.B. does 2 shock you, since A=[1 0 4;3 2 0] ? If so, remember that matlab is counting elements on columns first and then on lines, unlike C).
But you are looking for the OPPOSITE. So try negating the initial statement
>find(~A)
ans =

     3
     6
Once you have that, you just need to replace them.

>A(find(~A))=NaN

Hope it helps. There are many tutorials out there that can help you learn these useful matlab built in functions.
Regards,
Florin

Subject: find zeros and replace with NaN

From: jonathan

Date: 4 Apr, 2011 18:19:05

Message: 4 of 10

"Florin Neacsu" <fneacsu2@gmail.com> wrote in message <incsq1$bor$1@fred.mathworks.com>...
> "jonathan " <senator314159@hotmail.com> wrote in message <incrgo$imf$1@fred.mathworks.com>...
> > I have this matrix and I want to find all the zeros and change them to NaNs.
> > Example:
> > A=[1 0 4;3 2 0]
> > Should turn into
> > A=[1 NaN 4;3 2 NaN]
> >
> > can you tell me how
> >
> > Thank you for reply
>
> Hi Jonathan,
>
> I saw from you profile that you want to learn Matlab, so I am taking the liberty to go with you over your problem in more detail.
>
>
> You have a matrix and you are looking to change some specific values. You need to FIND those values first, right ? So,
> > doc find
>
> Find will provide you with the indices of nonzeros elements of A.
>
> >find(A)
>
> ans =
>
> 1
> 2
> 4
> 5
>
> (N.B. does 2 shock you, since A=[1 0 4;3 2 0] ? If so, remember that matlab is counting elements on columns first and then on lines, unlike C).
> But you are looking for the OPPOSITE. So try negating the initial statement
> >find(~A)
> ans =
>
> 3
> 6
> Once you have that, you just need to replace them.
>
> >A(find(~A))=NaN
>
> Hope it helps. There are many tutorials out there that can help you learn these useful matlab built in functions.
> Regards,
> Florin


Thanks a lot, that works great.
What kind of operation is that called? I am not sure why the ' ~ ' works or what it is called. ( If i put the ~ in matlab help it does not give me back anything)

- I suppose the ~ somehow just tells us where the zeros are?
is that right?

Subject: find zeros and replace with NaN

From: Florin Neacsu

Date: 4 Apr, 2011 18:40:19

Message: 5 of 10

"jonathan " <senator314159@hotmail.com> wrote in message <ind22p$c98$1@fred.mathworks.com>...
> "Florin Neacsu" <fneacsu2@gmail.com> wrote in message <incsq1$bor$1@fred.mathworks.com>...
> > "jonathan " <senator314159@hotmail.com> wrote in message <incrgo$imf$1@fred.mathworks.com>...
> > > I have this matrix and I want to find all the zeros and change them to NaNs.
> > > Example:
> > > A=[1 0 4;3 2 0]
> > > Should turn into
> > > A=[1 NaN 4;3 2 NaN]
> > >
> > > can you tell me how
> > >
> > > Thank you for reply
> >
> > Hi Jonathan,
> >
> > I saw from you profile that you want to learn Matlab, so I am taking the liberty to go with you over your problem in more detail.
> >
> >
> > You have a matrix and you are looking to change some specific values. You need to FIND those values first, right ? So,
> > > doc find
> >
> > Find will provide you with the indices of nonzeros elements of A.
> >
> > >find(A)
> >
> > ans =
> >
> > 1
> > 2
> > 4
> > 5
> >
> > (N.B. does 2 shock you, since A=[1 0 4;3 2 0] ? If so, remember that matlab is counting elements on columns first and then on lines, unlike C).
> > But you are looking for the OPPOSITE. So try negating the initial statement
> > >find(~A)
> > ans =
> >
> > 3
> > 6
> > Once you have that, you just need to replace them.
> >
> > >A(find(~A))=NaN
> >
> > Hope it helps. There are many tutorials out there that can help you learn these useful matlab built in functions.
> > Regards,
> > Florin
>
>
> Thanks a lot, that works great.
> What kind of operation is that called? I am not sure why the ' ~ ' works or what it is called. ( If i put the ~ in matlab help it does not give me back anything)
>
> - I suppose the ~ somehow just tells us where the zeros are?
> is that right?

Hi,

Have a look at http://www.mathworks.com/help/techdoc/ref/logicaloperatorselementwise.html

then test if you understood what ~ does; try something like
>for i=1:5, if (i>3), fprintf(1,'Yes'), else fprintf(1,'No'),end,end
and after that
>for i=1:5, if ~(i>3), fprintf(1,'Yes'), else fprintf(1,'No'),end,end

Once you got that, read over my intial reply and you should be good.

Florin

Subject: find zeros and replace with NaN

From: Matt J

Date: 4 Apr, 2011 18:54:04

Message: 6 of 10

"Florin Neacsu" <fneacsu2@gmail.com> wrote in message <incsq1$bor$1@fred.mathworks.com>...
> "jonathan " <senator314159@hotmail.com> wrote in message <incrgo$imf$1@fred.mathworks.com>...
>
> But you are looking for the OPPOSITE. So try negating the initial statement
> >find(~A)
> ans =
>
> 3
> 6
> Once you have that, you just need to replace them.
>
> >A(find(~A))=NaN
===============
 

FIND is actually unnecessary and sub-optimal here. As Husam pointed out,

A(~A)=nan

is sufficient. Using FIND just introduces the unnecessary overhead of converting from logical indices to linear indices.

Subject: find zeros and replace with NaN

From: Florin Neacsu

Date: 4 Apr, 2011 19:08:07

Message: 7 of 10

"Matt J" wrote in message <ind44c$j3g$1@fred.mathworks.com>...
> "Florin Neacsu" <fneacsu2@gmail.com> wrote in message <incsq1$bor$1@fred.mathworks.com>...
> > "jonathan " <senator314159@hotmail.com> wrote in message <incrgo$imf$1@fred.mathworks.com>...
> >
> > But you are looking for the OPPOSITE. So try negating the initial statement
> > >find(~A)
> > ans =
> >
> > 3
> > 6
> > Once you have that, you just need to replace them.
> >
> > >A(find(~A))=NaN
> ===============
>
>
> FIND is actually unnecessary and sub-optimal here. As Husam pointed out,
>
> A(~A)=nan
>
> is sufficient. Using FIND just introduces the unnecessary overhead of converting from logical indices to linear indices.

Matt J,

I know that, and even if didn't, as soon as you write it, Matlab tells you so. I wasn't aiming for the optimized way to code this, but I was just trying to help OP understand how he should tackle his problem.
I still believe for his purpose, and later use (lisibility) using find is appropriate.

Florin

Subject: find zeros and replace with NaN

From: Ms. Mat

Date: 11 Jan, 2013 03:47:08

Message: 8 of 10

"Matt J" wrote in message <ind44c$j3g$1@fred.mathworks.com>...
> "Florin Neacsu" <fneacsu2@gmail.com> wrote in message <incsq1$bor$1@fred.mathworks.com>...
> > "jonathan " <senator314159@hotmail.com> wrote in message <incrgo$imf$1@fred.mathworks.com>...
> >
> > But you are looking for the OPPOSITE. So try negating the initial statement
> > >find(~A)
> > ans =
> >
> > 3
> > 6
> > Once you have that, you just need to replace them.
> >
> > >A(find(~A))=NaN
> ===============
>
>
> FIND is actually unnecessary and sub-optimal here. As Husam pointed out,
>
> A(~A)=nan
>
> is sufficient. Using FIND just introduces the unnecessary overhead of converting from logical indices to linear indices.



Hi,

I just had a situation where I did A(~A) which returned an error message "NaN's cannot be converted to logicals." So I guess i have to use find here.

Subject: find zeros and replace with NaN

From: Bruno Luong

Date: 11 Jan, 2013 07:02:09

Message: 9 of 10

>
> I just had a situation where I did A(~A) which returned an error message "NaN's cannot be converted to logicals."

Try
A(A==0) = ...

Bruno

Subject: find zeros and replace with NaN

From: TideMan

Date: 11 Jan, 2013 08:05:53

Message: 10 of 10

On Friday, January 11, 2013 8:02:09 PM UTC+13, Bruno Luong wrote:
> >
>
> > I just had a situation where I did A(~A) which returned an error message "NaN's cannot be converted to logicals."
>
>
>
> Try
>
> A(A==0) = ...
>
>
>
> Bruno

Or, if the elements of A are not integers:
A(abs(A)<1e-6)= ...

Tags for 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