title: Why (in MATLAB) this code is faster?
body: <p>I have written some code in two different ways in MATLAB. Firstly, I used two for loops, which seems stupid at the first glance:</p>
  
  <pre><code>Initial = [zeros(10,1) ones(10,1)];
  
  for xpop=1:10
      for nvar=1:10
          Parent(xpop,nvar) = Initial(nvar,1)+(Initial(nvar,2)-Initial(nvar,1))*rand();
      end
  end
  </code></pre>
  
  <p>In the second scheme, I tried to do vectorized computation (I assumed it can be faster):</p>
  
  <pre><code>Parent = repmat(Initial(:,1),1,10) + rand(10,10).*(repmat(Initial(:,2),1,10)-repmat(Initial(:,1),1,10));
  </code></pre>
  
  <p>The elapsed time in three different run of the code can be seen following:</p>
  
  <pre><code>Elapsed time is 0.000456 seconds.
  Elapsed time is 0.006342 seconds.
  
  Elapsed time is 0.000457 seconds.
  Elapsed time is 0.006147 seconds.
  
  Elapsed time is 0.000471 seconds.
  Elapsed time is 0.006433 seconds.
  </code></pre>
  
  <p>Why is the first scheme faster than the second? Is it really doing two stupid for loops inside the '.*' command?</p>