title: Nested for loops extremely slow in MATLAB (preallocated)
body: <p>I am trying to learn MATLAB and one of the first problems I encountered was to guess the background from an image sequence with a static camera and moving objects. For a start I just want to do a mean or median on pixels over time, so it's <strong>just a single function I would like to apply to one of the rows of the 4 dimensional array</strong>.</p>
  
  <p>I have loaded my RGB images in a 4 dimensional array with the following dimensions:</p>
  
  <pre><code>uint8 [ num_images, width, height, RGB ]
  </code></pre>
  
  <p>Here is the function I wrote which includes 4 nested loops. I use <strong>preallocation</strong> but still, it is extremely slow. In C++ I believe this function could run at least 10x-20x faster, and I think on CUDA it could actually run in real time. In MATLAB it takes about 20 seconds with the 4 nested loops. My stack is 100 images with 640x480x3 dimensions. </p>
  
  <pre><code>function background = calc_background(stack)
  tic;
  
  si = size(stack,1);
  sy = size(stack,2);
  sx = size(stack,3);
  sc = size(stack,4);
  
  background = zeros(sy,sx,sc);
  A = zeros(si,1);
  
  for x = 1:sx
      for y = 1:sy
          for c = 1:sc
              for i = 1:si
                  A(i) = stack(i,y,x,c);
              end
              background(y,x,c) = median(A);
          end
      end
  end
  
  background = uint8(background);
  
  disp(toc);
  end
  </code></pre>
  
  <p>Could you tell me how to make this code much faster? I have tried experimenting with somehow getting the data directly from the array using only the indexes and it seems MUCH faster. It completes in <strong>3 seconds vs. 20 seconds</strong>, so that’s a 7x performance difference, just by writing a smaller function.</p>
  
  <pre><code>function background = calc_background2(stack)
      tic;
  
      % bad code, confusing
      % background = uint8(squeeze(median(stack(:, 1:size(stack,2), 1:size(stack,3), 1:3 ))));
  
      % good code (credits: Laurent)
      background=uint8((squeeze(median(stack,1)));
  
      disp(toc);
  end
  </code></pre>
  
  <p>So now I don't understand <strong>if MATLAB could be this fast then why is the nested loop version so slow?</strong> I am not making any dynamic resizing and MATLAB must be running the same 4 nested loops inside.</p>
  
  <p><strong>Why is this happening?</strong></p>
  
  <p>Is there any way to make nested loops run fast, like it would happen naturally in C++? </p>
  
  <p>Or should I get used to the idea of programming MATLAB in this crazy one line statements way to get optimal performance?</p>
  
  <p><strong>Update</strong></p>
  
  <p>Thank you for all the great answers, now I understand a lot more. My original code with <code>stack(:, 1:size(stack,2), 1:size(stack,3), 1:3 ))</code> didn't make any sense, it is exactly the same as <code>stack</code>, I was just lucky with median's default option of using the 1st dimension for its working range.</p>
  
  <p>I think it's better to ask how to write an efficient question in an other question, so I asked it here:</p>
  
  <p><a href="https://stackoverflow.com/q/7814220/518169">How to write vectorized functions in MATLAB</a></p>