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:
Delete some lines in a vector

Subject: Delete some lines in a vector

From: Camille Couzi

Date: 8 Nov, 2010 14:44:04

Message: 1 of 5

Hi matlabers,
I am a little bit bloked on a problem that seems really simple, though.
I have a vector, and I need to delete some rows of this vector in order to get a new vector.
For example, I have:
A=[ 5 9 1 8 3 1]';
These are the indexes that I want to delete:
ind=[2 6 3]

so at the end I would like to get:
newA=[5 8 3] (i have deleted the 2nd, 3rd and 6th element)

I have tried woth that code, but that don't works as at each step matlab delete one row and then my ind vector is wrong:

A=[ 5 9 1 8 3 1]';

ind=[2 6 3]; %las lineas q queremos eliminar
c=1;
for i=1:size(A,1)
    for j=1:size(ind,1)
        if i~=ind(j)
            newA(c,1)=A(i,1);
            c=c+1;
        end
    end
end
     
A
ind
newA

Thanks in advance for your help

Camille.

Subject: Delete some lines in a vector

From: someone

Date: 8 Nov, 2010 15:02:06

Message: 2 of 5

"Camille Couzi" <camillecouzi@yahoo.fr> wrote in message <ib92bk$88g$1@fred.mathworks.com>...
> Hi matlabers,
> I am a little bit bloked on a problem that seems really simple, though.
> I have a vector, and I need to delete some rows of this vector in order to get a new vector.
> For example, I have:
> A=[ 5 9 1 8 3 1]';
> These are the indexes that I want to delete:
> ind=[2 6 3]

>> A=[ 5 9 1 8 3 1]';
>> newA = A;
>> newA([2 6 3]) = []

newA =

     5
     8
     3

>
> so at the end I would like to get:
> newA=[5 8 3] (i have deleted the 2nd, 3rd and 6th element)
>
> I have tried woth that code, but that don't works as at each step matlab delete one row and then my ind vector is wrong:
>
> A=[ 5 9 1 8 3 1]';
>
> ind=[2 6 3]; %las lineas q queremos eliminar
> c=1;
> for i=1:size(A,1)
> for j=1:size(ind,1)
> if i~=ind(j)
> newA(c,1)=A(i,1);
> c=c+1;
> end
> end
> end
>
> A
> ind
> newA
>
> Thanks in advance for your help
>
> Camille.

Subject: Delete some lines in a vector

From: dpb

Date: 8 Nov, 2010 15:01:45

Message: 3 of 5

Camille Couzi wrote:
> Hi matlabers,
> I am a little bit bloked on a problem that seems really simple, though.
> I have a vector, and I need to delete some rows of this vector in order
> to get a new vector.
> For example, I have:
> A=[ 5 9 1 8 3 1]';
> These are the indexes that I want to delete:
> ind=[2 6 3]
>
> so at the end I would like to get:
> newA=[5 8 3] (i have deleted the 2nd, 3rd and 6th element)
>
> I have tried woth that code, but that don't works as at each step matlab
> delete one row and then my ind vector is wrong:
...

A(ind)=[]; % If don't need to save A; otherwise make a copy of A

Trick is to tell ML to do the full set of deletions at one swell foop...

--

Subject: Delete some lines in a vector

From: dpb

Date: 8 Nov, 2010 16:15:41

Message: 4 of 5

dpb wrote:
...

> Trick is to tell ML to do the full set of deletions at one swell foop...
...

And, as almost always I think of another tidbit later... :)

In general, the other trick when doing such index manipulation in
procedural language is to start the loop from the end and work backwards
to the beginning....then the reference to the earlier indices aren't
distorted.

You would want to sort your index list in descending order in that case
as well to make things easiest.

--

Subject: Delete some lines in a vector

From: Camille Couzi

Date: 8 Nov, 2010 19:44:05

Message: 5 of 5

Thank you so much! problem solved!

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