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 08:13:04 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 24
Message-ID: <gt6dqg$i52$1@fred.mathworks.com>
References: <1e6f5074-edce-417a-b0a5-b62db0fb033f@w31g2000prd.googlegroups.com> <128ed907-11c6-4e7a-a67a-736ececef060@s1g2000prd.googlegroups.com> <gt5sqm$7mc$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1240906384 18594 172.30.248.35 (28 Apr 2009 08:13:04 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 28 Apr 2009 08:13:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:535894


"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gt5sqm$7mc$1@fred.mathworks.com>...
>   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.
---------
  In case it is of interest to you, here is a vectorization of the previous for-loop which I mentioned I could "conceive" of.  Again, to generate m random compound poisson values using 'randn' as the independent random variables to be summed, do this:

 R = poissrnd(lambda,[1,m]); % <- Or your simulated poisson source
 p = cumsum([1,R]);
 r = randn(1,p(m+1)-1)+1; % <- Or some other ind. random variables
 s = [0,cumsum(r)];
 t = s(p(2:m+1))-s(p(1:m));

The row vector t will contain the desired compound poisson random values.  You can compare the timing on this with the simple for-loop version.

Roger Stafford