MATLAB: Column vector dot multiplied with a row vector. Multiple such operations..

dot product

I have a 5000×10, and another 5000×20 matrix. I want to dot multiply 5000 of each corresponding 10×1 & 1×20 vectors.
I want to get back 5000 of this operation. (10,1) .* (1,20) ==> (10×20). Thus into a (5000,10,20) matrix.
I can go into a loop for 5000 times. But is there a quicker mechanism in Matlab?
Thanks.

Best Answer

See
Or what about:
a = rand(5000, 10);
b = rand(5000, 20);
c = a .* reshape(b, [5000, 1, 20]); % >= Matlab R2016b
For older Matlab versions:
c = bsxfun(@times, a, reshape(b, [5000, 1, 20]));
Quicker would be to avoid the artificial inflate of the arrays: The resulting dyadic products are very redundant. Better reformulate the mathematical expressions such, that the single vectors are used.
Related Question