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:
removing indices from a vector

Subject: removing indices from a vector

From: Dimitar Dimitrov

Date: 27 Aug, 2007 07:36:16

Message: 1 of 3

Hello, I suppose I am asking a trivial question:

I want to remove some indices from a vector, like this:

W = [1 2 3 4 5 6 7]
arg_to_remove = [1 , 4, 6];
W(arg_to_remove) = 9999;
[a, b] = find(W~=9999);
W = W(b);

Personally I don't like the above method!! And I am looking
for a built in Matlab function (or a nice trick) that can do
that.

For example if the element to be removed is only one, I can
do it using:

W = [1 2 3 4 5 6 7]
arg_to_remove = 4;
W = W([1:arg_to_remove-1, arg_to_remove+1:length(W)]);

But if I have multiple indices in arg_to_remove the above
method doesn't work (of course I don't want to use a "for"
loop).

Thanks
Dimitar

Subject: removing indices from a vector

From: Inf

Date: 27 Aug, 2007 09:40:22

Message: 2 of 3

On 27 Aug, 08:36, "Dimitar Dimitrov" <mail_mi...@mathworks.com> wrote:
> Hello, I suppose I am asking a trivial question:
>
> I want to remove some indices from a vector, like this:
>
> W = [1 2 3 4 5 6 7]
> arg_to_remove = [1 , 4, 6];
> W(arg_to_remove) = 9999;
> [a, b] = find(W~=9999);
> W = W(b);
>
> Personally I don't like the above method!! And I am looking
> for a built in Matlab function (or a nice trick) that can do
> that.
>
> For example if the element to be removed is only one, I can
> do it using:
>
> W = [1 2 3 4 5 6 7]
> arg_to_remove = 4;
> W = W([1:arg_to_remove-1, arg_to_remove+1:length(W)]);
>
> But if I have multiple indices in arg_to_remove the above
> method doesn't work (of course I don't want to use a "for"
> loop).
>
> Thanks
> Dimitar


Dimitar,

Try

W = [1 2 3 4 5 6 7]
arg_to_remove = [1 , 4, 6];
W(arg_to_remove) = []


Hope this helps.

Inf

Subject: removing indices from a vector

From: Dimitar Dimitrov

Date: 27 Aug, 2007 10:03:41

Message: 3 of 3

Thank you very much!
How could I not think of this !!! :)

Dimitar

 Inf <infinitysquared@gmail.com> wrote in message
<1188207622.233379.151900@19g2000hsx.googlegroups.com>...
> On 27 Aug, 08:36, "Dimitar Dimitrov"
<mail_mi...@mathworks.com> wrote:
> > Hello, I suppose I am asking a trivial question:
> >
> > I want to remove some indices from a vector, like this:
> >
> > W = [1 2 3 4 5 6 7]
> > arg_to_remove = [1 , 4, 6];
> > W(arg_to_remove) = 9999;
> > [a, b] = find(W~=9999);
> > W = W(b);
> >
> > Personally I don't like the above method!! And I am looking
> > for a built in Matlab function (or a nice trick) that can do
> > that.
> >
> > For example if the element to be removed is only one, I can
> > do it using:
> >
> > W = [1 2 3 4 5 6 7]
> > arg_to_remove = 4;
> > W = W([1:arg_to_remove-1, arg_to_remove+1:length(W)]);
> >
> > But if I have multiple indices in arg_to_remove the above
> > method doesn't work (of course I don't want to use a "for"
> > loop).
> >
> > Thanks
> > Dimitar
>
>
> Dimitar,
>
> Try
>
> W = [1 2 3 4 5 6 7]
> arg_to_remove = [1 , 4, 6];
> W(arg_to_remove) = []
>
>
> Hope this helps.
>
> Inf
>

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