From: "Ulrik Nash" <>
Newsgroups: comp.soft-sys.matlab
Subject: Forming vector from another vector without looping
Date: Wed, 17 Mar 2010 13:02:06 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 21
Message-ID: <hnqjse$8b1$>
Reply-To: "Ulrik Nash" <>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: 1268830926 8545 (17 Mar 2010 13:02:06 GMT)
NNTP-Posting-Date: Wed, 17 Mar 2010 13:02:06 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 2106050
Xref: comp.soft-sys.matlab:617535

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?