title: MATLAB: How to vector-multiply two arrays of matrices?
body: <p>I have two 3-dimensional arrays, the first two dimensions of which represent matrices and the last one counts through a parameterspace, as a simple example take</p>
  
  <pre><code>A = repmat([1,2; 3,4], [1 1 4]);
  </code></pre>
  
  <p>(but assume <code>A(:,:,j)</code> is different for each <code>j</code>). How can one easily perform a per-<code>j</code> matrix multiplication of two such matrix-arrays <code>A</code> and <code>B</code>?</p>
  
  <pre><code>C = A; % pre-allocate, nan(size(A,1), size(B,2)) would be better but slower
  for jj = 1:size(A, 3)
    C(:,:,jj) = A(:,:,jj) * B(:,:,jj);
  end
  </code></pre>
  
  <p>certainly does the job, but if the third dimension is more like 1e3 elements this is very slow since it doesn't use MATLAB's vectorization. So, is there a faster way?</p>