Possible duplicate:
MATLAB: How is the vector multiplication of two arrays of matrices?
Is there a way to compress multidimensional tensors in Matlab?
For example, suppose I have two 3-dimensional arrays with the following sizes:
size(A) == [M,N,P] size(B) == [N,Q,P]
I want to enclose A and B in the second and first indices respectively. In other words, I want to consider A as an array of matrices of size [M,N] and B as an equal array of matrix lengths [N,Q] ; I want to multiply these arrays by elements (matrix by matrix) to get something of size [M,Q,P] .
I can do this through a for loop:
assert(size(A,2) == size(B,1)); assert(size(A,3) == size(B,3)); M = size(A,1); P = size(A,3); Q = size(B,2); C = zeros(M, Q, P); for ii = 1:size(A,3) C(:,:,ii) = A(:,:,ii) * B(:,:,ii); end
Is there a way to do this to avoid the for loop? (And maybe it works with arrays of an arbitrary number of dimensions?)