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 03:23:02 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 20
Message-ID: <gt5sqm$7mc$1@fred.mathworks.com>
References: <1e6f5074-edce-417a-b0a5-b62db0fb033f@w31g2000prd.googlegroups.com> <128ed907-11c6-4e7a-a67a-736ececef060@s1g2000prd.googlegroups.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 1240888982 7884 172.30.248.37 (28 Apr 2009 03:23:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 28 Apr 2009 03:23:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:535855


Bob Alvarez <ralvarez@spambob.net> wrote in message <128ed907-11c6-4e7a-a67a-736ececef060@s1g2000prd.googlegroups.com>...
> The idxs are actually samples of a poisson random variable. I did try
> to make a mask array using poly2mask from the image processing
> toolbox, but that is slower than doing the loop :-(

  I don't think vectorizing this procedure in this way is a very realistic aim in trying to simulate a truly compound poisson process.  There would have to be provision for an unlimited possible number of independent random variables to be summed which would mean a more or less unlimited number of columns in the desired matrix if you were using a fixed d matrix as in your example with a magic square.

  Suppose your independent random variables being summed are the results of the 'randn' output.  I would think you would want to proceed something like this:

 R = poissrnd(lambda,m); % <-Or whatever your simulated poisson source
 t = zeros(1,m);
 for k = 1:m
  t(k) = sum(randn(1,R(k)));
 end

This requires only as many samples from 'randn' as are called for by the sum of the counts in R.

  I can conceive of a way of vectorizing this latter method using differences among cumsums of the 'randn' outputs and indexed by the counts in R, but I foresee accuracy difficulties with that approach for very long sequences.  Without cumsumming I can see no effective way of eliminating the for-loops.

Roger Stafford