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 particular row from a matrix

Subject: remove particular row from a matrix

From: zheng

Date: 6 Jan, 2011 20:18:05

Message: 1 of 8

Here is a mat file stored with following values

43.5368190000000 -79.6658000000000 17.0700000000000 156
43.5368190000000 -79.6658000000000 17.0700000000000 147
43.5368190000000 -79.6658000000000 17.0700000000000 -1
43.5368190000000 -79.6658000000000 17.0700000000000 128
.
.
.
.
.
43.5368190000000 -79.6658000000000 17.0700000000000 -1
43.5368190000000 -79.6658000000000 17.0700000000000 158
43.5368190000000 -79.6658000000000 17.0700000000000 178
43.5368190000000 -79.6658000000000 17.0700000000000 -1

I want to remove particular rows which the X(:,4) is equal to -1. and then save the filtered matrix in a new file which has same structure n x 4 as the input mat file.
please give some hints to write the code.

thanks alot

Subject: remove particular row from a matrix

From: Sean de

Date: 6 Jan, 2011 20:47:04

Message: 2 of 8

"zheng " <fiendniu2003@hotmail.com> wrote in message <ig581t$et4$1@fred.mathworks.com>...
> Here is a mat file stored with following values
>
> 43.5368190000000 -79.6658000000000 17.0700000000000 156
> 43.5368190000000 -79.6658000000000 17.0700000000000 147
> 43.5368190000000 -79.6658000000000 17.0700000000000 -1
> 43.5368190000000 -79.6658000000000 17.0700000000000 128
> .
> .
> .
> .
> .
> 43.5368190000000 -79.6658000000000 17.0700000000000 -1
> 43.5368190000000 -79.6658000000000 17.0700000000000 158
> 43.5368190000000 -79.6658000000000 17.0700000000000 178
> 43.5368190000000 -79.6658000000000 17.0700000000000 -1
>
> I want to remove particular rows which the X(:,4) is equal to -1. and then save the filtered matrix in a new file which has same structure n x 4 as the input mat file.
> please give some hints to write the code.

x(x(:,4)==-1,:) = [];
Lear Logical Indexing!

Subject: remove particular row from a matrix

From: zheng

Date: 24 Jan, 2011 21:40:06

Message: 3 of 8

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ig59o8$5rn$1@fred.mathworks.com>...
> "zheng " <fiendniu2003@hotmail.com> wrote in message <ig581t$et4$1@fred.mathworks.com>...
> > Here is a mat file stored with following values
> >
> > 43.5368190000000 -79.6658000000000 17.0700000000000 156
> > 43.5368190000000 -79.6658000000000 17.0700000000000 147
> > 43.5368190000000 -79.6658000000000 17.0700000000000 -1
> > 43.5368190000000 -79.6658000000000 17.0700000000000 128
> > .
> > .
> > .
> > .
> > .
> > 43.5368190000000 -79.6658000000000 17.0700000000000 -1
> > 43.5368190000000 -79.6658000000000 17.0700000000000 158
> > 43.5368190000000 -79.6658000000000 17.0700000000000 178
> > 43.5368190000000 -79.6658000000000 17.0700000000000 -1
> >
> > I want to remove particular rows which the X(:,4) is equal to -1. and then save the filtered matrix in a new file which has same structure n x 4 as the input mat file.
> > please give some hints to write the code.
>
> x(x(:,4)==-1,:) = [];
> Lear Logical Indexing!


If I want to remove the particular rows which x(:,4)>178 && x(:,4)<147, is there any better way to do it?
or can I still use the last example to write:
x(x(:,4)>178,:)=[];
x(x(:,4)<147,:)=[];

Subject: remove particular row from a matrix

From: Paulo Silva

Date: 24 Jan, 2011 22:00:05

Message: 4 of 8

> If I want to remove the particular rows which x(:,4)>178 && x(:,4)<147, is there any better way to do it?
> or can I still use the last example to write:
> x(x(:,4)>178,:)=[];
> x(x(:,4)<147,:)=[];

find((x(:,4)>178) & (x(:,4)<147)) show give you their location

Subject: remove particular row from a matrix

From: Phil Goddard

Date: 25 Jan, 2011 03:00:07

Message: 5 of 8

I can't think of any numbers that are greater than 178 AND less than 147.

Are you sure you don't mean OR:

>> x((x(:,4)>178) | (x(:,4)<147)) = [];

Phil.

Subject: remove particular row from a matrix

From: Phil Goddard

Date: 25 Jan, 2011 04:45:06

Message: 6 of 8


That should of course be
>> x((x(:,4)>178) | (x(:,4)<147),:) = [];
 
Phil.

Subject: remove particular row from a matrix

From: zheng

Date: 25 Jan, 2011 18:02:04

Message: 7 of 8

"Phil Goddard" <philNOSPAM@goddardconsulting.ca> wrote in message <ihlkgi$b9q$1@fred.mathworks.com>...
>
> That should of course be
> >> x((x(:,4)>178) | (x(:,4)<147),:) = [];
>
> Phil.

Tks for reply

In my case, there are more than 10k points.
Here, I just list some as example. it does have number larger than 178 and less than 147.
the one you told me is not working.
there is no data left after applied it

Subject: remove particular row from a matrix

From: Phil Goddard

Date: 26 Jan, 2011 02:47:04

Message: 8 of 8

Works for me:

>> x = [...
43.5368190000000 -79.6658000000000 17.0700000000000 156
43.5368190000000 -79.6658000000000 17.0700000000000 147
43.5368190000000 -79.6658000000000 17.0700000000000 -1
43.5368190000000 -79.6658000000000 17.0700000000000 128
43.5368190000000 -79.6658000000000 17.0700000000000 -1
43.5368190000000 -79.6658000000000 17.0700000000000 158
43.5368190000000 -79.6658000000000 17.0700000000000 178
43.5368190000000 -79.6658000000000 17.0700000000000 -1
];

>> x((x(:,4)>178) | (x(:,4)<147),:) = []
x =
   43.5368 -79.6658 17.0700 156.0000
   43.5368 -79.6658 17.0700 147.0000
   43.5368 -79.6658 17.0700 158.0000
   43.5368 -79.6658 17.0700 178.0000

Phil.

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