Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!c18g2000prh.googlegroups.com!not-for-mail
From: jrenfree <jrenfree@gmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Help in vectorizing code
Date: Mon, 27 Apr 2009 12:42:02 -0700 (PDT)
Organization: http://groups.google.com
Lines: 40
Message-ID: <02cd4c0a-d994-4a8b-8dba-7c3bf32fce96@c18g2000prh.googlegroups.com>
References: <1e6f5074-edce-417a-b0a5-b62db0fb033f@w31g2000prd.googlegroups.com>
NNTP-Posting-Host: 137.110.142.168
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
X-Trace: posting.google.com 1240861322 27260 127.0.0.1 (27 Apr 2009 19:42:02 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Mon, 27 Apr 2009 19:42:02 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: c18g2000prh.googlegroups.com; posting-host=137.110.142.168; 
	posting-account=lVpOYAoAAAD2lyD71kM3JZW0H08VNtYu
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) 
	AppleWebKit/525.19 (KHTML, like Gecko) Chrome/1.0.154.48 Safari/525.19,gzip(gfe),gzip(gfe)
Xref: news.mathworks.com comp.soft-sys.matlab:535760


On Apr 27, 10:39=A0am, Bob <ralva...@spambob.net> wrote:
> 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 =3D magic(5)
>
> d =3D
>
> =A0 =A0 17 =A0 =A024 =A0 =A0 1 =A0 =A0 8 =A0 =A015
> =A0 =A0 23 =A0 =A0 5 =A0 =A0 7 =A0 =A014 =A0 =A016
> =A0 =A0 =A04 =A0 =A0 6 =A0 =A013 =A0 =A020 =A0 =A022
> =A0 =A0 10 =A0 =A012 =A0 =A019 =A0 =A021 =A0 =A0 3
> =A0 =A0 11 =A0 =A018 =A0 =A025 =A0 =A0 2 =A0 =A0 9
>
> >> idxs =3D round(linspace(1,3,5))
>
> idxs =3D
>
> =A0 =A0 =A01 =A0 =A0 2 =A0 =A0 2 =A0 =A0 3 =A0 =A0 3
>
> =A0for k=3D1:5
> =A0 =A0 totals(k) =3D sum(d(k,1:idxs(k)),2);
> =A0end
>
> >> totals'
>
> ans =3D
>
> =A0 =A0 17 =A0 =A028 =A0 =A010 =A0 =A041 =A0 =A054
>
> TIA

I'm not real sure how to do it using the idxs variable you create.  If
you have a way of making it more of a logical matrix, then it's easy:

idxs =3D [1 0 0 0 0;1 1 0 0 0;1 1 0 0 0;1 1 1 0 0;1 1 1 0 0];
sum(d.*idxs,2)