I would like to multiply each sub-block of a matrix A mxn with a matrix B pxq. For example A can be divided into k sub blocks each one of size mxp.
A = [A_1 A_2 ... A_k]
The resulting matrix will be C = [A_1*B A_2*B ... A_k*B] and I would like to do it efficiently.
What I have tried until now is:
C = A*kron(eye(k),B)
Edited: Daniel I think you are right. I tried 3 different ways. Computing a kronecker product seems to be a bad idea. Even the solution with the reshape works faster than the more compact kron solution.
tic
for i=1:k
C1(:,(i-1)*q+1:i*q) = A(:,(i-1)*p+1:i*p)*B;
end
toc
tic
C2 = A*kron(eye(k),B);
toc
tic
A = reshape(permute(reshape(A,m,p,[]),[1 3 2]),m*k,[]);
C3 = A*B;
C3 = reshape(permute(reshape(C3,m,k,[]),[1 3 2]),m,[]);
toc
When I look at your matrix multiplication code, you have perfectly optimized code within the loop. You can't beat matrix multiplication. Everything you could cut down is the overhead for the iteration, but compared to the long runtime of a matrix multiplication the overhead has absolutely no influence.
What you attempted to do would be the right strategy when the operation within the loop is trivial but the loop is iterated many times. If you take the following parameters, you will notice that your permute
solution has actually it's strength, but not for your problem dimensions:
q=1;p=1;n=1;m=1;
k=10^6
Kron totally fails. Your permute solution takes 0.006s
while the loop takes 1.512s