Got Questions? Get Answers.
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 delete some elements/rows in a vetor/matrix?

Subject: How to delete some elements/rows in a vetor/matrix?

From: MAO

Date: 10 Feb, 2011 17:24:04

Message: 1 of 5

I have a vector A=rand(100,1). I want to delete from this vector, the elements which are greater or equal to 0.5, for example. The vector A whose elements are smaller than 0.5 is now called vector B. How could I do to get vector B?

Subject: How to delete some elements/rows in a vetor/matrix?

From: Sean de

Date: 10 Feb, 2011 17:45:05

Message: 2 of 5

"MAO " <mao_nut@yahoo.com> wrote in message <ij16vk$1fa$1@fred.mathworks.com>...
> I have a vector A=rand(100,1). I want to delete from this vector, the elements which are greater or equal to 0.5, for example. The vector A whose elements are smaller than 0.5 is now called vector B. How could I do to get vector B?

%Delete Method: Glass half empty
B = A;
B(B>=0.5) = [];

%Keep Method: Glass half full
B = A(A<0.5);

Subject: How to delete some elements/rows in a vetor/matrix?

From: MAO

Date: 11 Feb, 2011 09:11:05

Message: 3 of 5

Thanks, your comment helps me much.
However, in the case where I have A = rand(100,3) and I want to keep only its rows whose the element of 2nd column > 0.4 and the element of 3rd column > 0.5 (the element of the 1st column is keep as it is generated), what can I do?


"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ij1871$jln$1@fred.mathworks.com>...
> "MAO " <mao_nut@yahoo.com> wrote in message <ij16vk$1fa$1@fred.mathworks.com>...
> > I have a vector A=rand(100,1). I want to delete from this vector, the elements which are greater or equal to 0.5, for example. The vector A whose elements are smaller than 0.5 is now called vector B. How could I do to get vector B?
>
> %Delete Method: Glass half empty
> B = A;
> B(B>=0.5) = [];
>
> %Keep Method: Glass half full
> B = A(A<0.5);

Subject: How to delete some elements/rows in a vetor/matrix?

From: Thomas Otterstaetter

Date: 11 Feb, 2011 09:42:04

Message: 4 of 5

A=rand(100,3);
B=A(A(:,2)>0.4 & A(:,3)>0.5 , :)

...and this can be continued with other columns or with 'or'...you might even just return certain columns with..
B=A(A(:,2)>0.4 & A(:,3)>0.5 , 1)

"MAO " <mao_nut@yahoo.com> wrote in message <ij2uf8$41j$1@fred.mathworks.com>...
> Thanks, your comment helps me much.
> However, in the case where I have A = rand(100,3) and I want to keep only its rows whose the element of 2nd column > 0.4 and the element of 3rd column > 0.5 (the element of the 1st column is keep as it is generated), what can I do?
>
>
> "Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ij1871$jln$1@fred.mathworks.com>...
> > "MAO " <mao_nut@yahoo.com> wrote in message <ij16vk$1fa$1@fred.mathworks.com>...
> > > I have a vector A=rand(100,1). I want to delete from this vector, the elements which are greater or equal to 0.5, for example. The vector A whose elements are smaller than 0.5 is now called vector B. How could I do to get vector B?
> >
> > %Delete Method: Glass half empty
> > B = A;
> > B(B>=0.5) = [];
> >
> > %Keep Method: Glass half full
> > B = A(A<0.5);

Subject: How to delete some elements/rows in a vetor/matrix?

From: MAO

Date: 11 Feb, 2011 11:15:07

Message: 5 of 5

Thank! The problem is solved :)

"Thomas Otterstaetter" <thomas.otterstaetter@gmail.com> wrote in message <ij309c$k8$1@fred.mathworks.com>...
> A=rand(100,3);
> B=A(A(:,2)>0.4 & A(:,3)>0.5 , :)
>
> ...and this can be continued with other columns or with 'or'...you might even just return certain columns with..
> B=A(A(:,2)>0.4 & A(:,3)>0.5 , 1)
>
> "MAO " <mao_nut@yahoo.com> wrote in message <ij2uf8$41j$1@fred.mathworks.com>...
> > Thanks, your comment helps me much.
> > However, in the case where I have A = rand(100,3) and I want to keep only its rows whose the element of 2nd column > 0.4 and the element of 3rd column > 0.5 (the element of the 1st column is keep as it is generated), what can I do?
> >
> >
> > "Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ij1871$jln$1@fred.mathworks.com>...
> > > "MAO " <mao_nut@yahoo.com> wrote in message <ij16vk$1fa$1@fred.mathworks.com>...
> > > > I have a vector A=rand(100,1). I want to delete from this vector, the elements which are greater or equal to 0.5, for example. The vector A whose elements are smaller than 0.5 is now called vector B. How could I do to get vector B?
> > >
> > > %Delete Method: Glass half empty
> > > B = A;
> > > B(B>=0.5) = [];
> > >
> > > %Keep Method: Glass half full
> > > B = A(A<0.5);

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