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:
remove row if contains NaN

Subject: remove row if contains NaN

From: Natalie Sin Hwee

Date: 16 Jul, 2010 14:32:05

Message: 1 of 8

Dear Mathworks users ^^

I have a matrix of 800x2
1 NaN
1 NaN
1 NaN
1 NaN
1 97
1 47
1 NaN
1 NaN
2 NaN
2 NaN
2 NaN
2 13
2 97
2 47
2 NaN
2 NaN


..... continued


I want to :

if row contains NAN, remove row completely so i end up with :
1 97
1 47
2 13
2 97
2 47

What function can i use? I've tried to 'find' the Nan's but it appears blank:
[r,c,v]=find(x==nan)

Thanks you!

Kind regards,
Natalie ^^

Subject: remove row if contains NaN

From: Sean

Date: 16 Jul, 2010 14:42:08

Message: 2 of 8

"Natalie Sin Hwee " <sin.ng09@imperial.ac.uk> wrote in message <i1pqh4$pmj$1@fred.mathworks.com>...
> Dear Mathworks users ^^
>
> I have a matrix of 800x2
> 1 NaN
> 1 NaN
> 1 NaN
> 1 NaN

> I want to :
>
> if row contains NAN, remove row completely so i end up with :
> 1 97
> 1 47
> 2 13
> 2 97
> 2 47

A = [1 2;3 4; nan 5];
A(~any(isnan(A),2),:)

Subject: remove row if contains NaN

From: Natalie Sin Hwee

Date: 16 Jul, 2010 15:52:06

Message: 3 of 8

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i1pr40$470$1@fred.mathworks.com>...
> "Natalie Sin Hwee " <sin.ng09@imperial.ac.uk> wrote in message <i1pqh4$pmj$1@fred.mathworks.com>...
> > Dear Mathworks users ^^
> >
> > I have a matrix of 800x2
> > 1 NaN
> > 1 NaN
> > 1 NaN
> > 1 NaN
>
> > I want to :
> >
> > if row contains NAN, remove row completely so i end up with :
> > 1 97
> > 1 47
> > 2 13
> > 2 97
> > 2 47
>
> A = [1 2;3 4; nan 5];
> A(~any(isnan(A),2),:)


Oh wow! thats a really good code!! :D so short ^^ THANKS SO MUCH!
thank youuuuuuuuuuuuu!!!

Subject: remove row if contains NaN

From: Nathan

Date: 16 Jul, 2010 19:21:08

Message: 4 of 8

On Jul 16, 8:52 am, "Natalie Sin Hwee " <sin.n...@imperial.ac.uk>
wrote:
> "Sean " <sean.dewol...@nospamplease.umit.maine.edu> wrote in message <i1pr40$47...@fred.mathworks.com>...
> > "Natalie Sin Hwee " <sin.n...@imperial.ac.uk> wrote in message <i1pqh4$pm...@fred.mathworks.com>...
> > > Dear Mathworks users ^^
>
> > > I have a matrix of 800x2
> > > 1     NaN
> > > 1     NaN
> > > 1     NaN
> > > 1     NaN
>
> > > I want to :
>
> > > if row contains NAN, remove row completely so i end up with :
> > > 1     97
> > > 1     47
> > > 2     13
> > > 2     97
> > > 2     47
>
> > A = [1 2;3 4; nan 5];
> > A(~any(isnan(A),2),:)
>
> Oh wow! thats a really good code!! :D so short ^^ THANKS SO MUCH!
> thank youuuuuuuuuuuuu!!!

Another solution:
x = [nan nan;1 nan; 1 1; 1 2; 2 nan; 2 1; nan 2]
x(logical(sum(x~=x,2)),:)=[]
%%%%%%%%%%%%%%%%
x =
     1 1
     1 2
     2 1

-Nathan

Subject: remove row if contains NaN

From: us

Date: 16 Jul, 2010 19:37:04

Message: 5 of 8

"Natalie Sin Hwee " <sin.ng09@imperial.ac.uk> wrote in message <i1pqh4$pmj$1@fred.mathworks.com>...
> Dear Mathworks users ^^
>
> I have a matrix of 800x2
> 1 NaN
> 1 NaN
> 1 NaN
> 1 NaN
> 1 97
> 1 47
> 1 NaN
> 1 NaN
> 2 NaN
> 2 NaN
> 2 NaN
> 2 13
> 2 97
> 2 47
> 2 NaN
> 2 NaN
>
>
> ..... continued
>
>
> I want to :
>
> if row contains NAN, remove row completely so i end up with :
> 1 97
> 1 47
> 2 13
> 2 97
> 2 47
>
> What function can i use? I've tried to 'find' the Nan's but it appears blank:
> [r,c,v]=find(x==nan)
>
> Thanks you!
>
> Kind regards,
> Natalie ^^

one of the many solutions

     m=[ % <- your data...
     ];
     m=m(~isnan(m(:,2)),:)
%{
% m =
          1 97
          1 47
          2 13
          2 97
          2 47
%}

us

Subject: remove row if contains NaN

From: John

Date: 25 Jul, 2012 21:18:23

Message: 6 of 8

These solutions only appear to work when selecting a particular column. This removes any row that has a NaN within any column:
%Matrix A
A(find(sum(isnan(A),2)==0),:)

Subject: remove row if contains NaN

From: dpb

Date: 25 Jul, 2012 22:00:24

Message: 7 of 8

On 7/25/2012 4:18 PM, John wrote:
...

> This removes any row that has a NaN within any column:
> %Matrix A
> A(find(sum(isnan(A),2)==0),:)

That _leaves_ rows w/ NaN's...the "==" should be "~="

Somewhat simpler is

A(any(isnan(A),2),:)=[];

--

Subject: remove row if contains NaN

From: dpb

Date: 26 Jul, 2012 01:03:52

Message: 8 of 8

On 7/25/2012 5:00 PM, dpb wrote:
> On 7/25/2012 4:18 PM, John wrote:
> ...
>
>> This removes any row that has a NaN within any column:
>> %Matrix A
>> A(find(sum(isnan(A),2)==0),:)
>
> That _leaves_ rows w/ NaN's...the "==" should be "~="
>
> Somewhat simpler is
>
> A(any(isnan(A),2),:)=[];
...

Or, if your intent was B=A(...) where B has no NaN then your logic
direction is correct just complement isnan() or use isfinite() (assuming
that it isn't desired to Inf in the result as well as NaN, of course)

B=A(~any(isnan(A),2),:)

or

B=A(all(isfinite(x),2),:)

--

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