Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vectorize the pairwise kronecker product in matlab

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
like image 536
user3030046 Avatar asked Mar 14 '23 22:03

user3030046


1 Answers

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);
like image 180
Forss Avatar answered Mar 23 '23 23:03

Forss