MATLAB: Computing A(:, m)*B(m, :) without a for loop

matrixmatrix manipulationproduct

Provided I have two matrices A and B of dimensions NxM and MxN I want to compute a matrix C of dimensions NxNxM such that each NxN slice is the result of the prduct A(:, m)*B(m, :). I know this can be obtained in the following way:
for m=1:M
C(:, :, m) = A(:, m)*B(m, :);
end
Is there a way of obtaining the same result without using a for loop (with the aim of making the computation faster)?

Best Answer

Ar=reshape(A,N,1,[]);
Br=reshape(B.',1,N,[]);
C=Ar.*Br;