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:
Forming vector from another vector without looping

Subject: Forming vector from another vector without looping

From: Ulrik Nash

Date: 17 Mar, 2010 13:02:06

Message: 1 of 3

Hi Everyone,

Suppose I have a horisontal vector of numbers A, for example:

A = [1 2 2 4 6 5]

And I have a vertical vector B of whole numbers, where non of these numbers are greater than the number of elements in A. For example:

B = [1 2 6]'

Now I wish to create (without looping) a vertical vector of numbers where each number is derived as follows (in this example):

C = [1 1+2 1+2+2+4+6+5]'

Notice that B defines how many of the numbers in A are added (from left to right), to form each element in C.

Can anyone tell me how to do this, without using loops?

Regards,

Ulrik.

Subject: Forming vector from another vector without looping

From: Yi Cao

Date: 17 Mar, 2010 13:52:05

Message: 2 of 3

One way to do it is as follows:

C = cumsum(A);
D = C(B);

HTH
Yi

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <hnqjse$8b1$1@fred.mathworks.com>...
> Hi Everyone,
>
> Suppose I have a horisontal vector of numbers A, for example:
>
> A = [1 2 2 4 6 5]
>
> And I have a vertical vector B of whole numbers, where non of these numbers are greater than the number of elements in A. For example:
>
> B = [1 2 6]'
>
> Now I wish to create (without looping) a vertical vector of numbers where each number is derived as follows (in this example):
>
> C = [1 1+2 1+2+2+4+6+5]'
>
> Notice that B defines how many of the numbers in A are added (from left to right), to form each element in C.
>
> Can anyone tell me how to do this, without using loops?
>
> Regards,
>
> Ulrik.

Subject: Forming vector from another vector without looping

From: Ulrik Nash

Date: 17 Mar, 2010 14:02:07

Message: 3 of 3

Thank you very much Yi, that is great!

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