Here's one approach using the powerful trio of bsxfun , permute and reshape -
M = bsxfun(@times,B,permute(A,[3 4 1 2])); out = reshape(permute(M,[1 3 2 4]),size(A,1)*size(B,1),[]);
If you are damn tuned to use repmat , do an M calculation with it, for example:
M = repmat(B,[1 1 size(A)]).*permute(repmat(A,[1 1 size(B)]),[3 4 1 2])
Check output by comparing with kron for total matrix sizes -
>> A = rand(4,5); >> B = rand(6,7); >> M = bsxfun(@times,B,permute(A,[3 4 1 2])); >> out = reshape(permute(M,[1 3 2 4]),size(A,1)*size(B,1),[]); >> out_kron = kron(A,B); >> max(abs(out(:) - out_kron(:))) ans = 0
Here matrix-multiplication and as such should be quite efficient -
[mA,nA] = size(A); [mB,nB] = size(B); out = reshape(permute(reshape(B(:)*A(:).',mB,nB,mA,nA),[1 3 2 4]),mA*mB,[])