Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Create a new vector from a current vector
Date: Mon, 5 Nov 2007 16:03:15 +0000 (UTC)
Organization: Universit&#228;tsSpital Z&#252;rich
Lines: 18
Message-ID: <fgnes3$mop$1@fred.mathworks.com>
References: <fgnbec$21u$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1194278595 23321 172.30.248.38 (5 Nov 2007 16:03:15 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 5 Nov 2007 16:03:15 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 11
Xref: news.mathworks.com comp.soft-sys.matlab:436116



Amir Safari:
<SNIP wants to sum subvecs of a larger one

others have shown you the typical way to do it...
one of the more versatile solutions is outlined below

% the data
     a=[1,3,3,4,2,4,3,5,5,2,4,3,7,4,2,4,1,4,2,3];
% - define the starting indices of your subvecs
     abeg=[1,6,7,10,18];
% the engine
     aend=[abeg(2:end)-1,numel(a)];
     r=arrayfun(@(b,e) sum(a(b:e)),abeg,aend);
% the result
     [abeg;aend] % the subvecs
     r

us