title: 2D convolution of slices of 3D matrix
body: <p>I'm trying to do a bunch of rolling sums over matrices in MATLAB. In order to avoid loops I've used <a href="http://www.mathworks.com/help/matlab/ref/repmat.html" rel="nofollow"><code>repmat</code></a> to layer my 2D matrices into a 3D structure. However, now the fast convolution function <a href="http://www.mathworks.com/help/matlab/ref/conv2.html" rel="nofollow"><code>conv2</code></a> can no longer be used for the accumulator. However, the N-dimensional convolution function (<a href="http://www.mathworks.com/help/matlab/ref/convn.html" rel="nofollow"><code>convn</code></a>) is not what I'm looking for either as it literally convolves all 3 dimensions. I want something that will do a 2D convolution on each slice and return a 3D matrix.</p>
  
  <p>Tiling the matrices in 2D instead of layering them in 3D won't work because it will corrupt the convolution edge cases. I could pad with zeros in between but then it starts getting kind of messy.</p>
  
  <p>In other words, without a for-loop, how can I perform the following:</p>
  
  <pre><code>A = ones(5,5,5);
  B = zeros(size(A));
  for i = 1 : size(A, 3)
      B(:,:,i) = conv2(A(:,:,i), ones(2), 'same');
  end
  </code></pre>
  
  <p>Thanks in advance for the help!</p>