title: How to optimize the following double for loop in octave?
body: <p>I have the following code in octave:</p>
  
  <pre><code>dist=0;
  for i = 1:length(x);        
      for j = 1:length(y);
          v  = x(i,:) - y(j,:);
          distvect(j) = norm(v);
      endfor 
      dist = dist + min(distvect);
  endfor
  </code></pre>
  
  <p>where x and y are matrices with size n x 2 and m x 2. My main problem: I need to run the code above several times. </p>
  
  <p>I'm pretty sure there is a way to optimize it with using probably just one matrix instead of the v vector each time in the inner for loop, but I could not find it. I searched online, I found an <a href="http://www.gnu.org/software/octave/doc/interpreter/Function-Application.html" rel="nofollow">arrayfun</a> function, which might help, but I could not figure out how to use. </p>
  
  <p>Thanks for helping,
  grus</p>