MATLAB: Multiplying certain dimensions of two 3D matrices

multiply 3d array

Hi
I've got two matrices, one 30x30x50 and one 30x30x80
I would like to multiply them in such a way that the result is a 50×80 matrix ie. an implicit sum over all of the 30×30 common elements.
eg. A*B = C
C(k,l) = A(i,j,k)*B(i,j,l) summed over all i, j
Is there a simple way to write this? I would rather avoid complications using "reshape".
Thanks, Mike

Best Answer

Idea of Sean de Wolski
C = squeeze(sum(sum(bsxfun(@times,a,permute(b,[1 2 4 3])))))
or
s = size(a)
C = squeeze(sum(bsxfun(@times,reshape(a,s(1)*s(2),[])',reshape(b,1,s(1)*s(2),[])),2))