MATLAB: How to multiply two matrices

basic

Hello,
For example I would like to multiply the following two matrices:
A = rand([10,10,10]);
B = rand([10,10,10]);
defined by:
for i=1:size(A,3)
C(:,:,i)= A(:,:,i)*B(:,:,i);
end
The question is: is this possible without the use of a for loop? Kind regards, Carlas

Best Answer

[m n p]= size(A);
o1 = ones(p,1));
out = cell2mat(cellfun(@mtimes,mat2cell(A,m,n,o1),mat2cell(B,m,n,o1),'un',0));
ADD
[m,n,p] = size(A);
out0 = bsxfun(@times,reshape(A,m,[]),reshape(permute(B,[1 3 2]),1,[],n));
out = reshape(sum(reshape(permute(out0,[1 3 2]),m,m,n,[]),3),m,n,[]);
or for out
out = permute(blockproc(out0,[m n],@(block_struct)sum(block_struct.data,2)),[1 3 2]);