Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Help in vectorizing code
Date: Tue, 28 Apr 2009 02:04:02 +0000 (UTC)
Organization: Battelle Energy Alliance (INL)
Lines: 11
Message-ID: <gt5o6i$t6q$1@fred.mathworks.com>
References: <1e6f5074-edce-417a-b0a5-b62db0fb033f@w31g2000prd.googlegroups.com> <gt5n8h$1kk$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1240884242 29914 172.30.248.37 (28 Apr 2009 02:04:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 28 Apr 2009 02:04:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 688530
Xref: news.mathworks.com comp.soft-sys.matlab:535843


% The data:
d = magic(5)
idxs = round(linspace(1,3,5));

% The engine.
F = zeros(size(d));
F(:,1) = 1;
F(sub2ind(size(d),1:5,idxs+1)) = -1;
sum(cumsum(F,2).*d,2)

Note:  The For loop might be faster still!