<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/250027</link>
    <title>MATLAB Central Newsreader - Help in vectorizing code</title>
    <description>Feed for thread: Help in vectorizing code</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Mon, 27 Apr 2009 17:39:02 -0400</pubDate>
      <title>Help in vectorizing code</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/250027#645690</link>
      <author>Bob</author>
      <description>For a Monte Carlo simulator of compound Poisson noise, I need to add a&lt;br&gt;
variable number of columns of each row of a matrix. So far, I have not&lt;br&gt;
been able to vectorize the for loop and any help will be appreciated.&lt;br&gt;
&lt;br&gt;
Here is an example:&lt;br&gt;
&amp;gt;&amp;gt; d = magic(5)&lt;br&gt;
&lt;br&gt;
d =&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;17    24     1     8    15&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;23     5     7    14    16&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;4     6    13    20    22&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;10    12    19    21     3&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;11    18    25     2     9&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; idxs = round(linspace(1,3,5))&lt;br&gt;
&lt;br&gt;
idxs =&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1     2     2     3     3&lt;br&gt;
&lt;br&gt;
&amp;nbsp;for k=1:5&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;totals(k) = sum(d(k,1:idxs(k)),2);&lt;br&gt;
&amp;nbsp;end&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; totals'&lt;br&gt;
&lt;br&gt;
ans =&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;17    28    10    41    54&lt;br&gt;
&lt;br&gt;
TIA</description>
    </item>
    <item>
      <pubDate>Mon, 27 Apr 2009 19:42:02 -0400</pubDate>
      <title>Re: Help in vectorizing code</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/250027#645711</link>
      <author>jrenfree</author>
      <description>On Apr 27, 10:39=A0am, Bob &amp;lt;ralva...@spambob.net&amp;gt; wrote:&lt;br&gt;
&amp;gt; For a Monte Carlo simulator of compound Poisson noise, I need to add a&lt;br&gt;
&amp;gt; variable number of columns of each row of a matrix. So far, I have not&lt;br&gt;
&amp;gt; been able to vectorize the for loop and any help will be appreciated.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Here is an example:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; d =3D magic(5)&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; d =3D&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; =A0 =A0 17 =A0 =A024 =A0 =A0 1 =A0 =A0 8 =A0 =A015&lt;br&gt;
&amp;gt; =A0 =A0 23 =A0 =A0 5 =A0 =A0 7 =A0 =A014 =A0 =A016&lt;br&gt;
&amp;gt; =A0 =A0 =A04 =A0 =A0 6 =A0 =A013 =A0 =A020 =A0 =A022&lt;br&gt;
&amp;gt; =A0 =A0 10 =A0 =A012 =A0 =A019 =A0 =A021 =A0 =A0 3&lt;br&gt;
&amp;gt; =A0 =A0 11 =A0 =A018 =A0 =A025 =A0 =A0 2 =A0 =A0 9&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; idxs =3D round(linspace(1,3,5))&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; idxs =3D&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; =A0 =A0 =A01 =A0 =A0 2 =A0 =A0 2 =A0 =A0 3 =A0 =A0 3&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; =A0for k=3D1:5&lt;br&gt;
&amp;gt; =A0 =A0 totals(k) =3D sum(d(k,1:idxs(k)),2);&lt;br&gt;
&amp;gt; =A0end&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; totals'&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; ans =3D&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; =A0 =A0 17 =A0 =A028 =A0 =A010 =A0 =A041 =A0 =A054&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; TIA&lt;br&gt;
&lt;br&gt;
I'm not real sure how to do it using the idxs variable you create.  If&lt;br&gt;
you have a way of making it more of a logical matrix, then it's easy:&lt;br&gt;
&lt;br&gt;
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];&lt;br&gt;
sum(d.*idxs,2)</description>
    </item>
    <item>
      <pubDate>Tue, 28 Apr 2009 00:53:11 -0400</pubDate>
      <title>Re: Help in vectorizing code</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/250027#645774</link>
      <author>Bob Alvarez</author>
      <description>On Apr 27, 12:42=A0pm, jrenfree &amp;lt;jrenf...@gmail.com&amp;gt; wrote:&lt;br&gt;
&amp;gt; I'm not real sure how to do it using the idxs variable you create. =A0If&lt;br&gt;
&amp;gt; you have a way of making it more of a logical matrix, then it's easy:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; 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];&lt;br&gt;
&amp;gt; sum(d.*idxs,2)&lt;br&gt;
&lt;br&gt;
The idxs are actually samples of a poisson random variable. I did try&lt;br&gt;
to make a mask array using poly2mask from the image processing&lt;br&gt;
toolbox, but that is slower than doing the loop :-(</description>
    </item>
    <item>
      <pubDate>Tue, 28 Apr 2009 01:40:19 -0400</pubDate>
      <title>Re: Help in vectorizing code</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/250027#645790</link>
      <author>Darren Rowland</author>
      <description>&lt;br&gt;
This is one way to make the mask quickly&lt;br&gt;
mask = zeros(size(d));&lt;br&gt;
mask(:,idxs) = 1;&lt;br&gt;
mask = cumsum(mask,1);&lt;br&gt;
&lt;br&gt;
Hth&lt;br&gt;
Darren</description>
    </item>
    <item>
      <pubDate>Tue, 28 Apr 2009 01:48:01 -0400</pubDate>
      <title>Re: Help in vectorizing code</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/250027#645792</link>
      <author>Darren Rowland</author>
      <description>Sorry, I think the line above&lt;br&gt;
mask(:,idxs) = 1;&lt;br&gt;
will not work as desired. Should probably be instead&lt;br&gt;
&lt;br&gt;
mask([1:5],idxs) = 1;&lt;br&gt;
&lt;br&gt;
Haven't the chance to check though.</description>
    </item>
    <item>
      <pubDate>Tue, 28 Apr 2009 02:04:02 -0400</pubDate>
      <title>Re: Help in vectorizing code</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/250027#645794</link>
      <author>Matt Fig</author>
      <description>% The data:&lt;br&gt;
d = magic(5)&lt;br&gt;
idxs = round(linspace(1,3,5));&lt;br&gt;
&lt;br&gt;
% The engine.&lt;br&gt;
F = zeros(size(d));&lt;br&gt;
F(:,1) = 1;&lt;br&gt;
F(sub2ind(size(d),1:5,idxs+1)) = -1;&lt;br&gt;
sum(cumsum(F,2).*d,2)&lt;br&gt;
&lt;br&gt;
Note:  The For loop might be faster still!</description>
    </item>
    <item>
      <pubDate>Tue, 28 Apr 2009 03:23:02 -0400</pubDate>
      <title>Re: Help in vectorizing code</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/250027#645806</link>
      <author>Roger Stafford</author>
      <description>Bob Alvarez &amp;lt;ralvarez@spambob.net&amp;gt; wrote in message &amp;lt;128ed907-11c6-4e7a-a67a-736ececef060@s1g2000prd.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; The idxs are actually samples of a poisson random variable. I did try&lt;br&gt;
&amp;gt; to make a mask array using poly2mask from the image processing&lt;br&gt;
&amp;gt; toolbox, but that is slower than doing the loop :-(&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;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.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;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:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;R = poissrnd(lambda,m); % &amp;lt;-Or whatever your simulated poisson source&lt;br&gt;
&amp;nbsp;t = zeros(1,m);&lt;br&gt;
&amp;nbsp;for k = 1:m&lt;br&gt;
&amp;nbsp;&amp;nbsp;t(k) = sum(randn(1,R(k)));&lt;br&gt;
&amp;nbsp;end&lt;br&gt;
&lt;br&gt;
This requires only as many samples from 'randn' as are called for by the sum of the counts in R.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;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.&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Tue, 28 Apr 2009 04:28:34 -0400</pubDate>
      <title>Re: Help in vectorizing code</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/250027#645819</link>
      <author>Bob Alvarez</author>
      <description>On Apr 27, 8:23=A0pm, &quot;Roger Stafford&quot;&lt;br&gt;
&amp;lt;ellieandrogerxy...@mindspring.com.invalid&amp;gt; wrote:&lt;br&gt;
&amp;gt; =A0 I don't think vectorizing this procedure in this way is a very realis=&lt;br&gt;
tic aim in trying to simulate a truly compound poisson process. =A0There wo=&lt;br&gt;
uld have to be provision for an unlimited possible number of independent ra=&lt;br&gt;
ndom variables to be summed which would mean a more or less unlimited numbe=&lt;br&gt;
r of columns in the desired matrix if you were using a fixed d matrix as in=&lt;br&gt;
&amp;nbsp;your example with a magic square.&lt;br&gt;
&amp;gt;&lt;br&gt;
For every set of trials there will be a maximum number of summed&lt;br&gt;
terms, which determines the size of the matrix. I agree that in&lt;br&gt;
principle the maximum can be arbitrarily large although the&lt;br&gt;
probability of that is infinitesimally small. Using a matrix is&lt;br&gt;
certainly more wasteful but is feasible for small values (say of the&lt;br&gt;
order or hundreds) of the Poisson parameter. Probably larger values&lt;br&gt;
are practically indistinguishable from normal.&lt;br&gt;
&lt;br&gt;
Thanks to all for the ideas.&lt;br&gt;
&lt;br&gt;
I wrote a mex function called sum2idx.c, which I list below. It is not&lt;br&gt;
'bullet proof' but it gets the job done if used carefully.&lt;br&gt;
&lt;br&gt;
Bob&lt;br&gt;
&lt;br&gt;
sum2idx.c&lt;br&gt;
-------------------------&lt;br&gt;
/*&lt;br&gt;
rowsums =3D sum2idx(M,idx)&lt;br&gt;
sums rows of matrix M from 1:idx&lt;br&gt;
where idx is a vector of length =3D # of rows of M&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;REA 4/26/09&lt;br&gt;
*/&lt;br&gt;
&lt;br&gt;
#define V4_COMPAT&lt;br&gt;
#include &amp;lt;matrix.h&amp;gt;  /* Matlab matrices */&lt;br&gt;
#include &amp;lt;mex.h&amp;gt;&lt;br&gt;
&lt;br&gt;
#include &amp;lt;stddef.h&amp;gt;  /* NULL */&lt;br&gt;
&lt;br&gt;
#define notDblMtx(it) (!mxIsNumeric(it) || !mxIsDouble(it) ||&lt;br&gt;
mxIsSparse(it) || mxIsComplex(it))&lt;br&gt;
&lt;br&gt;
void mexFunction(int nlhs,	     /* Num return vals on lhs */&lt;br&gt;
		 mxArray *plhs[],    /* Matrices on lhs      */&lt;br&gt;
		 int nrhs,	     /* Num args on rhs    */&lt;br&gt;
		 const mxArray *prhs[]     /* Matrices on rhs */&lt;br&gt;
		 )&lt;br&gt;
&amp;nbsp;&amp;nbsp;{&lt;br&gt;
&amp;nbsp;&amp;nbsp;register double temp, sumval;&lt;br&gt;
&amp;nbsp;&amp;nbsp;register double *mtx,*idx,*outArray;&lt;br&gt;
&amp;nbsp;&amp;nbsp;register int i, k,idxval,nrows,ncols,nrows_idx,ncols_idx;&lt;br&gt;
&amp;nbsp;&amp;nbsp;mxArray *arg;&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;if (nrhs !=3D 2) mexErrMsgTxt(&quot;sum2idx: requires 2 arguments.&quot;);&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;/* ARG 1: MATRIX  */&lt;br&gt;
&amp;nbsp;&amp;nbsp;arg =3D prhs[0];&lt;br&gt;
&amp;nbsp;&amp;nbsp;if notDblMtx(arg) mexErrMsgTxt(&quot;sum2idx: M arg must be a real non-&lt;br&gt;
sparse matrix.&quot;);&lt;br&gt;
&amp;nbsp;&amp;nbsp;mtx =3D mxGetPr(arg);&lt;br&gt;
&amp;nbsp;&amp;nbsp;nrows =3D (int) mxGetM(arg);&lt;br&gt;
&amp;nbsp;&amp;nbsp;ncols =3D (int) mxGetN(arg);&lt;br&gt;
&amp;nbsp;&amp;nbsp;if ((nrows*ncols) =3D=3D 0) mexErrMsgTxt(&quot;sum2idx: M must be a non-empty&lt;br&gt;
matrix.&quot;);&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;/* ARG 2: idx vector  */&lt;br&gt;
&amp;nbsp;&amp;nbsp;arg =3D prhs[1];&lt;br&gt;
&amp;nbsp;&amp;nbsp;if notDblMtx(arg) mexErrMsgTxt(&quot;sum2idx: idx arg must be a real non-&lt;br&gt;
sparse 1D vector.&quot;);&lt;br&gt;
&amp;nbsp;&amp;nbsp;idx =3D mxGetPr(arg);&lt;br&gt;
&amp;nbsp;&amp;nbsp;nrows_idx =3D (int) mxGetM(arg);&lt;br&gt;
&amp;nbsp;&amp;nbsp;ncols_idx =3D (int) mxGetN(arg);&lt;br&gt;
&amp;nbsp;&amp;nbsp;/* assume the calling program forces idx to be a column vector*/&lt;br&gt;
&amp;nbsp;&amp;nbsp;if(ncols_idx!=3D1) mexErrMsgTxt(&quot;sum2idx: idx must be 1 D column&lt;br&gt;
vector&quot;);&lt;br&gt;
&amp;nbsp;&amp;nbsp;if(nrows_idx!=3Dnrows) mexErrMsgTxt(&quot;sum2idx: idx must be same length&lt;br&gt;
as number of rows of M&quot;);&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;/* do the sum */&lt;br&gt;
&amp;nbsp;&amp;nbsp;plhs[0] =3D (mxArray *) mxCreateDoubleMatrix((int)nrows,1,mxREAL);&lt;br&gt;
&amp;nbsp;&amp;nbsp;if (plhs[0] =3D=3D NULL) mexErrMsgTxt(&quot;sum2idx: Error allocating result&lt;br&gt;
matrix&quot;);&lt;br&gt;
&amp;nbsp;&amp;nbsp;//Get a pointer to the data space in our newly allocated memory&lt;br&gt;
&amp;nbsp;&amp;nbsp;outArray =3D mxGetPr(plhs[0]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;for (i=3D0; i&amp;lt;nrows; i++){&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;outArray[i] =3D 0.0;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;idxval =3D (int)idx[i];&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(idxval&amp;gt;ncols) mexErrMsgTxt(&quot;sum2idx: idx must be &amp;lt;=3D number of&lt;br&gt;
columns of M&quot;);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(idxval&amp;gt;0){ //do the sum&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for(k=3D0;k&amp;lt;idxval;k++) outArray[i] +=3D mtx[(k*ncols)+i];&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;
&amp;nbsp;&amp;nbsp;}&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;return;</description>
    </item>
    <item>
      <pubDate>Tue, 28 Apr 2009 08:13:04 -0400</pubDate>
      <title>Re: Help in vectorizing code</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/250027#645845</link>
      <author>Roger Stafford</author>
      <description>&quot;Roger Stafford&quot; &amp;lt;ellieandrogerxyzzy@mindspring.com.invalid&amp;gt; wrote in message &amp;lt;gt5sqm$7mc$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt;   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:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;  R = poissrnd(lambda,m); % &amp;lt;-Or whatever your simulated poisson source&lt;br&gt;
&amp;gt;  t = zeros(1,m);&lt;br&gt;
&amp;gt;  for k = 1:m&lt;br&gt;
&amp;gt;   t(k) = sum(randn(1,R(k)));&lt;br&gt;
&amp;gt;  end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; This requires only as many samples from 'randn' as are called for by the sum of the counts in R.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   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.&lt;br&gt;
---------&lt;br&gt;
&amp;nbsp;&amp;nbsp;In case it is of interest to you, here is a vectorization of the previous for-loop which I mentioned I could &quot;conceive&quot; of.  Again, to generate m random compound poisson values using 'randn' as the independent random variables to be summed, do this:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;R = poissrnd(lambda,[1,m]); % &amp;lt;- Or your simulated poisson source&lt;br&gt;
&amp;nbsp;p = cumsum([1,R]);&lt;br&gt;
&amp;nbsp;r = randn(1,p(m+1)-1)+1; % &amp;lt;- Or some other ind. random variables&lt;br&gt;
&amp;nbsp;s = [0,cumsum(r)];&lt;br&gt;
&amp;nbsp;t = s(p(2:m+1))-s(p(1:m));&lt;br&gt;
&lt;br&gt;
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.&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Tue, 28 Apr 2009 15:59:02 -0400</pubDate>
      <title>Re: Help in vectorizing code</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/250027#645935</link>
      <author>Roger Stafford</author>
      <description>&quot;Roger Stafford&quot; &amp;lt;ellieandrogerxyzzy@mindspring.com.invalid&amp;gt; wrote in message &amp;lt;gt6dqg$i52$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt;  R = poissrnd(lambda,[1,m]); % &amp;lt;- Or your simulated poisson source&lt;br&gt;
&amp;gt;  p = cumsum([1,R]);&lt;br&gt;
&amp;gt;  r = randn(1,p(m+1)-1)+1; % &amp;lt;- Or some other ind. random variables&lt;br&gt;
&amp;gt;  s = [0,cumsum(r)];&lt;br&gt;
&amp;gt;  t = s(p(2:m+1))-s(p(1:m));&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;Please remove that +1 from the third line.  To be compatible with my other statements it should not be there.  It had to do with something I was temporarily testing.  The third line should read:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;r = randn(1,p(m+1)-1); % &amp;lt;- Or some other ind. random variables</description>
    </item>
    <item>
      <pubDate>Wed, 29 Apr 2009 04:12:58 -0400</pubDate>
      <title>Re: Help in vectorizing code</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/250027#646034</link>
      <author>Bob Alvarez</author>
      <description>Some updates:&lt;br&gt;
&lt;br&gt;
1.  There was a bug in the C code I posted. A new version is at the&lt;br&gt;
end of this post.&lt;br&gt;
2. I timed Roger's vectorized code, the for loop and the Mex code.&lt;br&gt;
Results:&lt;br&gt;
Elapsed time is 0.037361 seconds. % vectorized&lt;br&gt;
Elapsed time is 0.019795 seconds. % for loop&lt;br&gt;
Elapsed time is 0.027539 seconds. % Mex code&lt;br&gt;
Doh! Apparently the JIT compiler is so good it beats the other 2&lt;br&gt;
methods. The test matlab code is:&lt;br&gt;
&lt;br&gt;
---&lt;br&gt;
%% code from Roger Stafford of news group&lt;br&gt;
lambda = 1000;&lt;br&gt;
m=1000;&lt;br&gt;
&amp;nbsp;R = poissrnd(lambda,[1,m]); % &amp;lt;- Or your simulated poisson source&lt;br&gt;
&amp;nbsp;tic&lt;br&gt;
&amp;nbsp;p = cumsum([1,R]);&lt;br&gt;
&amp;nbsp;r = randn(1,p(m+1)-1); % &amp;lt;- Or some other ind. random variables&lt;br&gt;
&amp;nbsp;s = [0,cumsum(r)];&lt;br&gt;
&amp;nbsp;t = s(p(2:m+1))-s(p(1:m));&lt;br&gt;
toc&lt;br&gt;
clear p r s&lt;br&gt;
&lt;br&gt;
%% do with for loop&lt;br&gt;
tic&lt;br&gt;
t2 = zeros(1,m);&lt;br&gt;
for k = 1:m&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;t2(k) = sum(randn(1,R(k)));&lt;br&gt;
end&lt;br&gt;
toc&lt;br&gt;
&lt;br&gt;
%% do with mex code&lt;br&gt;
tic&lt;br&gt;
d = randn(m,max(R)+1);&lt;br&gt;
t3 = sum2idx(d,R(:));&lt;br&gt;
toc&lt;br&gt;
&lt;br&gt;
----- sum2idx.c (updated) -----&lt;br&gt;
/*&lt;br&gt;
rowsums = sum2idx(M,idx)&lt;br&gt;
sums rows of matrix M from 1:idx&lt;br&gt;
where idx is a vector of length = # of rows of M&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;REA 4/28/09&lt;br&gt;
*/&lt;br&gt;
&lt;br&gt;
#define V4_COMPAT&lt;br&gt;
#include &amp;lt;matrix.h&amp;gt;  /* Matlab matrices */&lt;br&gt;
#include &amp;lt;mex.h&amp;gt;&lt;br&gt;
&lt;br&gt;
#include &amp;lt;stddef.h&amp;gt;  /* NULL */&lt;br&gt;
&lt;br&gt;
#define notDblMtx(it) (!mxIsNumeric(it) || !mxIsDouble(it) ||&lt;br&gt;
mxIsSparse(it) || mxIsComplex(it))&lt;br&gt;
&lt;br&gt;
void mexFunction(int nlhs,	     /* Num return vals on lhs */&lt;br&gt;
		 mxArray *plhs[],    /* Matrices on lhs      */&lt;br&gt;
		 int nrhs,	     /* Num args on rhs    */&lt;br&gt;
		 const mxArray *prhs[]     /* Matrices on rhs */&lt;br&gt;
		 )&lt;br&gt;
&amp;nbsp;&amp;nbsp;{&lt;br&gt;
&amp;nbsp;&amp;nbsp;double temp, sumval;&lt;br&gt;
&amp;nbsp;&amp;nbsp;double *mtx,*idx,*outArray;&lt;br&gt;
&amp;nbsp;&amp;nbsp;int kcol, krow,idxval,nrows,ncols,nrows_idx,ncols_idx;&lt;br&gt;
&amp;nbsp;&amp;nbsp;mxArray *arg;&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;if (nrhs != 2) mexErrMsgTxt(&quot;sum2idx: requires 2 arguments.&quot;);&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;/* ARG 1: MATRIX  */&lt;br&gt;
&amp;nbsp;&amp;nbsp;arg = prhs[0];&lt;br&gt;
&amp;nbsp;&amp;nbsp;if notDblMtx(arg) mexErrMsgTxt(&quot;sum2idx: M arg must be a real non-&lt;br&gt;
sparse matrix.&quot;);&lt;br&gt;
&amp;nbsp;&amp;nbsp;mtx = mxGetPr(arg);&lt;br&gt;
&amp;nbsp;&amp;nbsp;nrows = (int) mxGetM(arg);&lt;br&gt;
&amp;nbsp;&amp;nbsp;ncols = (int) mxGetN(arg);&lt;br&gt;
&amp;nbsp;&amp;nbsp;if ((nrows*ncols) == 0) mexErrMsgTxt(&quot;sum2idx: M must be a non-empty&lt;br&gt;
matrix.&quot;);&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;/* ARG 2: idx vector  */&lt;br&gt;
&amp;nbsp;&amp;nbsp;arg = prhs[1];&lt;br&gt;
&amp;nbsp;&amp;nbsp;if notDblMtx(arg) mexErrMsgTxt(&quot;sum2idx: idx arg must be a real non-&lt;br&gt;
sparse 1D vector.&quot;);&lt;br&gt;
&amp;nbsp;&amp;nbsp;idx = mxGetPr(arg);&lt;br&gt;
&amp;nbsp;&amp;nbsp;nrows_idx = (int) mxGetM(arg);&lt;br&gt;
&amp;nbsp;&amp;nbsp;ncols_idx = (int) mxGetN(arg);&lt;br&gt;
&amp;nbsp;&amp;nbsp;/* assume the calling program forces idx to be a column vector*/&lt;br&gt;
&amp;nbsp;&amp;nbsp;if(ncols_idx!=1) mexErrMsgTxt(&quot;sum2idx: idx must be 1 D column&lt;br&gt;
vector&quot;);&lt;br&gt;
&amp;nbsp;&amp;nbsp;if(nrows_idx!=nrows) mexErrMsgTxt(&quot;sum2idx: idx must be same length&lt;br&gt;
as number of rows of M&quot;);&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;/* do the sum */&lt;br&gt;
&amp;nbsp;&amp;nbsp;plhs[0] = (mxArray *) mxCreateDoubleMatrix((int)nrows,1,mxREAL);&lt;br&gt;
&amp;nbsp;&amp;nbsp;if (plhs[0] == NULL) mexErrMsgTxt(&quot;sum2idx: Error allocating result&lt;br&gt;
matrix&quot;);&lt;br&gt;
&amp;nbsp;&amp;nbsp;//Get a pointer to the data space in our newly allocated memory&lt;br&gt;
&amp;nbsp;&amp;nbsp;outArray = mxGetPr(plhs[0]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;for(krow=0;krow&amp;lt;nrows;krow++){&lt;br&gt;
&amp;nbsp;&amp;nbsp;	sumval=0.0;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;idxval = (int)idx[krow];&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(idxval&amp;gt;ncols) mexErrMsgTxt(&quot;sum2idx: idx must be &amp;lt;= number&lt;br&gt;
of columns of M&quot;);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(idxval&amp;gt;0){ //do the sum&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for(kcol=0;kcol&amp;lt;idxval;kcol++){&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;sumval += mtx[(kcol*nrows)+krow];&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;outArray[krow] = sumval;&lt;br&gt;
&amp;nbsp;&amp;nbsp;}&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;return;&lt;br&gt;
}</description>
    </item>
    <item>
      <pubDate>Wed, 29 Apr 2009 06:13:04 -0400</pubDate>
      <title>Re: Help in vectorizing code</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/250027#646042</link>
      <author>Jos </author>
      <description>Bob &amp;lt;ralvarez@spambob.net&amp;gt; wrote in message &amp;lt;1e6f5074-edce-417a-b0a5-b62db0fb033f@w31g2000prd.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; For a Monte Carlo simulator of compound Poisson noise, I need to add a&lt;br&gt;
&amp;gt; variable number of columns of each row of a matrix. So far, I have not&lt;br&gt;
&amp;gt; been able to vectorize the for loop and any help will be appreciated.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Here is an example:&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; d = magic(5)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; d =&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;     17    24     1     8    15&lt;br&gt;
&amp;gt;     23     5     7    14    16&lt;br&gt;
&amp;gt;      4     6    13    20    22&lt;br&gt;
&amp;gt;     10    12    19    21     3&lt;br&gt;
&amp;gt;     11    18    25     2     9&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; idxs = round(linspace(1,3,5))&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; idxs =&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;      1     2     2     3     3&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;  for k=1:5&lt;br&gt;
&amp;gt;     totals(k) = sum(d(k,1:idxs(k)),2);&lt;br&gt;
&amp;gt;  end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; totals'&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; ans =&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;     17    28    10    41    54&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; TIA&lt;br&gt;
&lt;br&gt;
What about simple indexing after applying cumsum:&lt;br&gt;
&lt;br&gt;
% data&lt;br&gt;
d = magic(5)&lt;br&gt;
idxs = round(linspace(1,3,5))&lt;br&gt;
&lt;br&gt;
% engine&lt;br&gt;
d2 = cumsum(d,2) ;&lt;br&gt;
totals = d2(sub2ind(size(d2),1:numel(idxs),idxs))&lt;br&gt;
&lt;br&gt;
hth&lt;br&gt;
Jos</description>
    </item>
    <item>
      <pubDate>Wed, 29 Apr 2009 18:43:04 -0400</pubDate>
      <title>Re: Help in vectorizing code</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/250027#646231</link>
      <author>Bob Alvarez</author>
      <description>On Apr 28, 11:13&#160;pm, &quot;Jos &quot; &amp;lt;#10...@fileexchange.com&amp;gt; wrote:&lt;br&gt;
&amp;gt; Bob &amp;lt;ralva...@spambob.net&amp;gt; wrote in message &amp;lt;1e6f5074-edce-417a-b0a5-b62d=&lt;br&gt;
b0fb0...@w31g2000prd.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; For a Monte Carlo simulator of compound Poisson noise, I need to add a&lt;br&gt;
&amp;gt; &amp;gt; variable number of columns of each row of a matrix. So far, I have not&lt;br&gt;
&amp;gt; &amp;gt; been able to vectorize the for loop and any help will be appreciated.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; Here is an example:&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt;&amp;gt; d = magic(5)&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; d =&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; &#160; &#160; 17 &#160; &#160;24 &#160; &#160; 1 &#160; &#160; 8 &#160; &#160;15&lt;br&gt;
&amp;gt; &amp;gt; &#160; &#160; 23 &#160; &#160; 5 &#160; &#160; 7 &#160; &#160;14 &#160; &#160;16&lt;br&gt;
&amp;gt; &amp;gt; &#160; &#160; &#160;4 &#160; &#160; 6 &#160; &#160;13 &#160; &#160;20 &#160; &#160;22&lt;br&gt;
&amp;gt; &amp;gt; &#160; &#160; 10 &#160; &#160;12 &#160; &#160;19 &#160; &#160;21 &#160; &#160; 3&lt;br&gt;
&amp;gt; &amp;gt; &#160; &#160; 11 &#160; &#160;18 &#160; &#160;25 &#160; &#160; 2 &#160; &#160; 9&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt;&amp;gt; idxs = round(linspace(1,3,5))&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; idxs =&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; &#160; &#160; &#160;1 &#160; &#160; 2 &#160; &#160; 2 &#160; &#160; 3 &#160; &#160; 3&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; &#160;for k=1:5&lt;br&gt;
&amp;gt; &amp;gt; &#160; &#160; totals(k) = sum(d(k,1:idxs(k)),2);&lt;br&gt;
&amp;gt; &amp;gt; &#160;end&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; &amp;gt;&amp;gt; totals'&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; ans =&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; &#160; &#160; 17 &#160; &#160;28 &#160; &#160;10 &#160; &#160;41 &#160; &#160;54&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; TIA&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; What about simple indexing after applying cumsum:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; % data&lt;br&gt;
&amp;gt; d = magic(5)&lt;br&gt;
&amp;gt; idxs = round(linspace(1,3,5))&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; % engine&lt;br&gt;
&amp;gt; d2 = cumsum(d,2) ;&lt;br&gt;
&amp;gt; totals = d2(sub2ind(size(d2),1:numel(idxs),idxs))&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; hth&lt;br&gt;
&amp;gt; Jos&lt;br&gt;
&lt;br&gt;
Good idea. Timed the following code:&lt;br&gt;
%% code from Jos&lt;br&gt;
tic&lt;br&gt;
d = cumsum(randn(m,ceil(max(R))),2);&lt;br&gt;
s = d(sub2ind(size(d),1:numel(R),R));&lt;br&gt;
toc&lt;br&gt;
&amp;nbsp;result:&lt;br&gt;
Elapsed time is 0.027088 seconds. % Jos&lt;br&gt;
Elapsed time is 0.019429 seconds. % for loop&lt;br&gt;
The for loop seems to win because it does not have to compute a full&lt;br&gt;
matrix, just the specified length random variable at each trial.</description>
    </item>
    <item>
      <pubDate>Wed, 29 Apr 2009 00:23:03 -0400</pubDate>
      <title>Re: Help in vectorizing code</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/250027#646251</link>
      <author>Bob Alvarez</author>
      <description>On Apr 28, 8:59&#160;am, &quot;Roger Stafford&quot;&lt;br&gt;
&amp;lt;ellieandrogerxy...@mindspring.com.invalid&amp;gt; wrote:&lt;br&gt;
&amp;gt; &quot;Roger Stafford&quot; &amp;lt;ellieandrogerxy...@mindspring.com.invalid&amp;gt; wrote in mes=&lt;br&gt;
sage &amp;lt;gt6dqg$i5...@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; &#160;R = poissrnd(lambda,[1,m]); % &amp;lt;- Or your simulated poisson source&lt;br&gt;
&amp;gt; &amp;gt; &#160;p = cumsum([1,R]);&lt;br&gt;
&amp;gt; &amp;gt; &#160;r = randn(1,p(m+1)-1)+1; % &amp;lt;- Or some other ind. random variables&lt;br&gt;
&amp;gt; &amp;gt; &#160;s = [0,cumsum(r)];&lt;br&gt;
&amp;gt; &amp;gt; &#160;t = s(p(2:m+1))-s(p(1:m));&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &#160; Please remove that +1 from the third line. &#160;To be compatible with m=&lt;br&gt;
y other statements it should not be there. &#160;It had to do with something I=&lt;br&gt;
&amp;nbsp;was temporarily testing. &#160;The third line should read:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &#160;r = randn(1,p(m+1)-1); % &amp;lt;- Or some other ind. random variables&lt;br&gt;
&lt;br&gt;
Very interesting. Thanks for your effort.&lt;br&gt;
&lt;br&gt;
I tested the code with the following and got some interesting results:&lt;br&gt;
-------&lt;br&gt;
%% code from Roger Stafford of news group&lt;br&gt;
tic&lt;br&gt;
lambda = 1000;&lt;br&gt;
m=1000;&lt;br&gt;
&amp;nbsp;R = poissrnd(lambda,[1,m]); % &amp;lt;- Or your simulated poisson source&lt;br&gt;
&amp;nbsp;p = cumsum([1,R]);&lt;br&gt;
&amp;nbsp;r = randn(1,p(m+1)-1); % &amp;lt;- Or some other ind. random variables&lt;br&gt;
&amp;nbsp;s = [0,cumsum(r)];&lt;br&gt;
&amp;nbsp;t = s(p(2:m+1))-s(p(1:m));&lt;br&gt;
toc&lt;br&gt;
&lt;br&gt;
%% do with for loop&lt;br&gt;
tic&lt;br&gt;
R2 = poissrnd(lambda,[1,m]); % generate these for timing but do not&lt;br&gt;
use them so sums are the same&lt;br&gt;
t2 = zeros(1,m);&lt;br&gt;
for k = 1:m&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;t2(k) = sum(randn(1,R(k)));&lt;br&gt;
end&lt;br&gt;
toc&lt;br&gt;
------------------&lt;br&gt;
RESULTS:&lt;br&gt;
Elapsed time is 0.042971 seconds.&lt;br&gt;
Elapsed time is 0.023447 seconds.&lt;br&gt;
The for loop is faster!!&lt;br&gt;
Apparently the JIT compiler is able to handle the for loop code&lt;br&gt;
effectively.&lt;br&gt;
&lt;br&gt;
Anyway, thanks to all who answered.</description>
    </item>
  </channel>
</rss>

