Path: news.mathworks.com!newsfeed-00.mathworks.com!nlpi057.nbdc.sbc.com!prodigy.net!border1.nntp.dca.giganews.com!nntp.giganews.com!postnews.google.com!d25g2000prn.googlegroups.com!not-for-mail
From: Bob Alvarez <ralvarez@spambob.net>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Help in vectorizing code
Date: Tue, 28 Apr 2009 17:23:03 -0700 (PDT)
Organization: http://groups.google.com
Lines: 49
Message-ID: <974716f4-4e75-4150-9ece-35ef82c1cbc4@d25g2000prn.googlegroups.com>
References: <1e6f5074-edce-417a-b0a5-b62db0fb033f@w31g2000prd.googlegroups.com> 
	<128ed907-11c6-4e7a-a67a-736ececef060@s1g2000prd.googlegroups.com> 
	<gt5sqm$7mc$1@fred.mathworks.com> <gt6dqg$i52$1@fred.mathworks.com> 
	<gt7946$81g$1@fred.mathworks.com>
NNTP-Posting-Host: 216.103.212.87
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
X-Trace: posting.google.com 1241047106 15086 127.0.0.1 (29 Apr 2009 23:18:26 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Wed, 29 Apr 2009 23:18:26 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: d25g2000prn.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.10) 
	Gecko/2009042316 Firefox/3.0.10 (.NET CLR 3.5.30729),gzip(gfe),gzip(gfe)
Bytes: 3026
Xref: news.mathworks.com comp.soft-sys.matlab:536300


On Apr 28, 8:59 am, "Roger Stafford"
<ellieandrogerxy...@mindspring.com.invalid> wrote:
> "Roger Stafford" <ellieandrogerxy...@mindspring.com.invalid> wrote in mes=
sage <gt6dqg$i5...@fred.mathworks.com>...
> >  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));
>
>   Please remove that +1 from the third line.  To be compatible with m=
y other statements it should not be there.  It had to do with something I=
 was temporarily testing.  The third line should read:
>
>  r = randn(1,p(m+1)-1); % <- Or some other ind. random variables

Very interesting. Thanks for your effort.

I tested the code with the following and got some interesting results:
-------
%% code from Roger Stafford of news group
tic
lambda = 1000;
m=1000;
 R = poissrnd(lambda,[1,m]); % <- Or your simulated poisson source
 p = cumsum([1,R]);
 r = randn(1,p(m+1)-1); % <- Or some other ind. random variables
 s = [0,cumsum(r)];
 t = s(p(2:m+1))-s(p(1:m));
toc

%% do with for loop
tic
R2 = poissrnd(lambda,[1,m]); % generate these for timing but do not
use them so sums are the same
t2 = zeros(1,m);
for k = 1:m
     t2(k) = sum(randn(1,R(k)));
end
toc
------------------
RESULTS:
Elapsed time is 0.042971 seconds.
Elapsed time is 0.023447 seconds.
The for loop is faster!!
Apparently the JIT compiler is able to handle the for loop code
effectively.

Anyway, thanks to all who answered.