Path: news.mathworks.com!newsfeed-00.mathworks.com!solaris.cc.vt.edu!news.vt.edu!news.glorb.com!news2.glorb.com!postnews.google.com!w31g2000prd.googlegroups.com!not-for-mail
From: Bob <ralvarez@spambob.net>
Newsgroups: comp.soft-sys.matlab
Subject: Help in vectorizing code
Date: Mon, 27 Apr 2009 10:39:02 -0700 (PDT)
Organization: http://groups.google.com
Lines: 32
Message-ID: <1e6f5074-edce-417a-b0a5-b62db0fb033f@w31g2000prd.googlegroups.com>
NNTP-Posting-Host: 216.103.212.87
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
X-Trace: posting.google.com 1240853943 4154 127.0.0.1 (27 Apr 2009 17:39:03 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Mon, 27 Apr 2009 17:39:03 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: w31g2000prd.googlegroups.com; posting-host=216.103.212.87; 
	posting-account=pNrldAoAAAB5GlVBDJqvQa-_ca5yFHCO
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.9) 
	Gecko/2009040821 Firefox/3.0.9 (.NET CLR 3.5.30729),gzip(gfe),gzip(gfe)
Xref: news.mathworks.com comp.soft-sys.matlab:535739


For a Monte Carlo simulator of compound Poisson noise, I need to add a
variable number of columns of each row of a matrix. So far, I have not
been able to vectorize the for loop and any help will be appreciated.

Here is an example:
>> d = magic(5)

d =

    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2     9

>> idxs = round(linspace(1,3,5))

idxs =

     1     2     2     3     3

 for k=1:5
    totals(k) = sum(d(k,1:idxs(k)),2);
 end

>> totals'

ans =

    17    28    10    41    54

TIA