Suppose there are two matrices of the same size, and I want to calculate the summation of their column-wise kronecker product. Due to sometimes the column size is quite large so that the speed could be very slow. Thus, is there anyway to vectorize this function or any function may help reducing the complexity in matlab? Thanks in advance.
The corresponding matlab code with a for-loop is provided below, and the answer of d is the interested output:
A = rand(3,7);
B = rand(3,7);
d = zeros(size(A,1)*size(B,1),1);
for i=1:size(A,2)
d = d + kron(A(:,i),B(:,i));
end
解决方案
Using the rewriting of the Kronecker product given by Daniels answer
e=zeros(size(B,1),size(A,1));
for i=1:size(A,2)
e = e + B(:,i)*A(:,i).';
end
e=reshape(e,[],1);
we say that
C = A'
and thus
for i=1:m
e = e + B(:,i)*C(i,:);
end
which is the definition of the matrix product
B*C.
In conclusion the problem can thus be solved by the simple matrix product
d = reshape(B*A',[],1);