<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/163842</link>
    <title>MATLAB Central Newsreader - Vectorization, For loop</title>
    <description>Feed for thread: Vectorization, For loop</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2008 by The 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>The MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Fri, 15 Feb 2008 18:00:18 -0500</pubDate>
      <title>Re: Vectorization, For loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/163842#415380</link>
      <author>Roger Stafford</author>
      <description>"Anish Goorah" &amp;lt;anish.goorah@gmail.com&amp;gt; wrote in message &amp;lt;fp434u$n1m&lt;br&gt;
$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Any brillant ideas as to how I avoid this double loop:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; %%Initialization&lt;br&gt;
&amp;gt; iter = 50000;&lt;br&gt;
&amp;gt; simlen = 81;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; dPt(:,(1:iter)) = 0.0142;%Initial Price Growth&lt;br&gt;
&amp;gt; price(:,(1:iter)) = 100000;%Initial Price&lt;br&gt;
&amp;gt; stdev = 0.25;&lt;br&gt;
&amp;gt; rho = 0.5;&lt;br&gt;
&amp;gt; c = 0.05;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; %Simluation&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; dist = stdev*randn(simlen+1,iter);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; for i=1:iter&lt;br&gt;
&amp;gt;     for j=1:simlen&lt;br&gt;
&amp;gt;         dPt(j+1,i) = c+rho*dPt(j,i)+dist(j,i);&lt;br&gt;
&amp;gt;         price(j+1,:) = price(j,:).*(1+dPt(j+1,:));&lt;br&gt;
&amp;gt;     end&lt;br&gt;
&amp;gt; end&lt;br&gt;
-------&lt;br&gt;
&amp;nbsp;&amp;nbsp;I need some clarification here, Anish.&lt;br&gt;
&lt;br&gt;
1. When you write dPt(:,(1:iter)) = 0.0142, has the array 'dPt' been previously &lt;br&gt;
defined, and if so, what is its size?  Is it simlen+1 by iter?  If it hasn't been &lt;br&gt;
defined, it is not a good idea to force dPt to have its rows allocated one at a &lt;br&gt;
time, as it would be in the inner loop.  That slows down computation &lt;br&gt;
markedly.  You need to allocate the entire array initially even if all but the first &lt;br&gt;
row start out as zeros, as in:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;dPt = zeros(simlen+1,iter); dPt(1,:) = 0.0142;&lt;br&gt;
&lt;br&gt;
2. In the inner loop did you mean to write&lt;br&gt;
&lt;br&gt;
&amp;nbsp;price(j+1,i) = price(j,i).*(1+dPt(j+1,i)) ?&lt;br&gt;
&lt;br&gt;
What you have at present does not make a lot of sense.  All the columns of &lt;br&gt;
'price' are being defined in terms of 'dPt' columns, some of which have &lt;br&gt;
apparently not yet been calculated, at least in a meaningful way.&lt;br&gt;
&lt;br&gt;
3. The way you have set up this iteration, the first row of dPt does not have &lt;br&gt;
any effect on values in the 'price' array.  Was that your intent?&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;Assuming your answers to the above are what I think they will be, you can &lt;br&gt;
use just one for-loop for computing 'dPt' and vectorize 'price' completely.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;for j=1:simlen&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;dPt(j+1,:) = c+rho*dPt(j,:)+dist(j,:);&lt;br&gt;
&amp;nbsp;end&lt;br&gt;
&amp;nbsp;price = cumprod([price(1,:);dPt(2:simlen+1,:)+1],1);&lt;br&gt;
&lt;br&gt;
Roger Stafford&lt;br&gt;
&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Fri, 15 Feb 2008 13:13:02 -0500</pubDate>
      <title>Vectorization, For loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/163842#415294</link>
      <author>Anish Goorah</author>
      <description>Any brillant ideas as to how I avoid this double loop:&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
%%Initialization&lt;br&gt;
iter = 50000;&lt;br&gt;
simlen = 81;&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
dPt(:,(1:iter)) = 0.0142;%Initial Price Growth&lt;br&gt;
price(:,(1:iter)) = 100000;%Initial Price&lt;br&gt;
stdev = 0.25;&lt;br&gt;
rho = 0.5;&lt;br&gt;
c = 0.05;&lt;br&gt;
&lt;br&gt;
%Simluation&lt;br&gt;
&lt;br&gt;
dist = stdev*randn(simlen+1,iter);&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
for i=1:iter&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for j=1:simlen&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;dPt(j+1,i) = c+rho*dPt(j,i)+dist(j,i);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;price(j+1,:) = price(j,:).*(1+dPt(j+1,:));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
end&lt;br&gt;
</description>
    </item>
  </channel>
</rss>
