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 alternate elements in a vector

Subject: remove alternate elements in a vector

From: jay jay

Date: 4 Jul, 2010 04:37:03

Message: 1 of 7

Hi

I have a very huge vector:

A = [ 0 3 1 4 2 6 4 ...]

I want to remove the alternate elements from it such that it becomes:

A = [ 0 1 2 4 ...]

I can't think of a smart way to do it.

Thanks in advance.

Subject: remove alternate elements in a vector

From: Matt Fig

Date: 4 Jul, 2010 04:45:06

Message: 2 of 7

A(2:2:end) = []

Subject: remove alternate elements in a vector

From: jay jay

Date: 4 Jul, 2010 05:09:03

Message: 3 of 7

"Matt Fig" <spamanon@yahoo.com> wrote in message <i0p3ki$i6t$1@fred.mathworks.com>...
> A(2:2:end) = []

Oh yea! thanks!

Subject: remove alternate elements in a vector

From: shahnaz fatima

Date: 4 Jul, 2010 15:07:16

Message: 4 of 7

can u tell me what does A(2:2:end) = [] mean?
how can i ask help for that in matlab.

Subject: remove alternate elements in a vector

From: Faraz Afzal

Date: 4 Jul, 2010 15:22:05

Message: 5 of 7

"shahnaz fatima" <shahnaz1981fat@gmail.com> wrote in message <i0q834$svi$1@fred.mathworks.com>...
> can u tell me what does A(2:2:end) = [] mean?
> how can i ask help for that in matlab.

Dear Shahnaz You will not be able to find out help on this...
INstead you can try---- > doc colon

The explanation of it is as follows :

A = [ 0 3 1 4 2 6 4 ...]

I want to remove the alternate elements from it such that it becomes:

A = [ 0 1 2 4 ...]
Solution was
A(2:2:end) = []

Means out of this A matrix Give me elements Starting from 2 element and with increment of every second element..

A(2:2:end) from 2nd element to the end with increment of 2nd element..

Regards,
Muhammad Faraz

Subject: remove alternate elements in a vector

From: Matt Fig

Date: 4 Jul, 2010 16:11:22

Message: 6 of 7

"shahnaz fatima" <shahnaz1981fat@gmail.com> wrote in message <i0q834$svi$1@fred.mathworks.com>...
> can u tell me what does A(2:2:end) = [] mean?
> how can i ask help for that in matlab.

A(2:2:end) Means this: Start with the second element and include every other element after that until the end of the array is reached. docsearch('linear indexing')

= [] Means this: Set the elements which were addressed on the left hand side to empty arrays. Setting an element to empty basically means to delete it. docsearch('empty')

Subject: remove alternate elements in a vector

From: Jan Simon

Date: 4 Jul, 2010 19:50:06

Message: 7 of 7

Dear Shahnaz,

> can u tell me what does A(2:2:end) = [] mean?

"= []" means "remove".
"2:2:end" means "alternate elements"
"A( )" means "of a vector".

I know, this has been explained already. But I found the exactness of Matt Fig's translation impressing.

You can find something to this commands in Help->Getting started->Manipulating matrices (name might depends on the release).

Good luck and welcome to Matlab, Jan

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