MATLAB: How to multiply each row of a matrix with every row of another matrix elementwise

elementwisematrixmultiplicationtensor

I have two matrices A and B with size k x m and l x m, respectively. I want to multiply each row of matrix A with each row of matrix B elementwise to get a tensor of size k x l x m.

Best Answer

bsxfun(@times,permute(A,[1,3,2]),permute(B,[3,1,2]))
or using MATAB R2016b and newer:
permute(A,[1,3,2]) .* permute(B,[3,1,2])