MATLAB: How to tensorise columns of two matrices to produce a 3D array

bsxfunmtimesxvectorisation

I'm looking to tensorise the columns of two matrices. If A has dimension m x p and B has dimension m x r, I'm looking to compute C with dimension m x p x r where C(:,j,k) = A(:,j).*B(:,k).
Initially I used nested for loops through j and k, which worked well. However I need to perform this operation millions of times so thought vectorisation might speed things up.
I have managed to achieve this by first computing repmat(A,r,1).*reshape(B,[],1) and reshaping the result into C. Is there anyway this can be achieved faster using the functions bsxfun or mtimesx? I've read that these functions might enable me to not use repmat, which is slow for large m or r.

Best Answer

A .* permute(B, [1 3 2])
or visit in ancient versions of MATLAB:
bsxfun(@times, A, permute(B, [1 3 2]))