title: MATLAB parfor slicing a 3D array
body: <p>I'm trying to speed up my code using <code>parfor</code>. The purpose of the code is to slide a 3D square window on a 3D image and for each block of <code>mxmxm</code> apply a function.</p>
  
  <p>I wrote this code:</p>
  
  <pre><code>function [ o_image ] = SlidingWindow( i_image, i_padSize, i_fun, i_options )
  %SLIDINGWINDOW Summary of this function goes here
  %   Detailed explanation goes here
  
  o_image = zeros(size(i_image,1),size(i_image,2),size(i_image,3));
  i_image = padarray(i_image,i_padSize,'symmetric');
  i_padSize = num2cell(i_padSize);
  [m,n,p] = deal(i_padSize{:});
  [row,col,depth] = size(i_image);
  windowShape = i_options.windowShape;
  mask = i_options.mask;
  parfor (i = m+1:row-m,i_options.cores)
      temp = i_image(i-m:i+m,:,:);
      for j = n+1:col-n
          for h = p+1:depth-p
              ii = i-m;
              jj = j-n;
              hh = h-p;
              temp = temp(:,j-n:j+n, h-p:h+p);
              o_image(ii,jj,hh) = parfeval(i_fun, temp, windowShape, mask);
          end
      end
  end
  
  end
  </code></pre>
  
  <p>I get one warning and one error that I don't understand how to solve.
  The warning says:</p>
  
  <blockquote>
    <p>the entire array or structure 'i_image' is a broadcast variable.</p>
  </blockquote>
  
  <p>The error says:</p>
  
  <blockquote>
    <p>the PARFOR loop can not run due to the way variable 'o_image' is used.</p>
  </blockquote>
  
  <p>I don't understand how to fix these two things. Any help is greatly appreciated!</p>