Avoiding a for loop in Matlab with Kronecker product

Viewed 62

If I have two sequences A and B containing the elements a1, a2, ... and b1,b2, ... where a_i and b_i are of dimension 1xn and 1xm, respectively, then I want to make a new sequence C which contains: a_i(1)*b_i, a_i(2)*b_i, ... , a_i(n)*b_i. So for the ith element of C, I want to have the Kronecker product of the elements a_i and b_i. I want to code this in Matlab, but without a for-loop. For the case when b_i are scalars, the desired result is achieved with

C = A.*B

However, this does not work for non-scalar b_i's. So what I do now is for sequences of length L:

C = [];
for ii = 1:L
    C = [C; kron(A(ii,:),B(ii,:))];
end

But I have the idea that this must be possible without a for loop. Here is my code using the symbolic toolbox:

clc; clear;

L = 5;

syms('a1',[L,1]);
syms('a2',[L,1]);
syms('b1',[L,1]);
syms('b2',[L,1]);

A = [a1,a2];
B = [b1,b2];

C1 = A.*B % only for size(B,2)=1

C2 = [];

for ii = 1:L
    C2 = [C2;kron(A(ii,:),B(ii,:))];
end
C2

C3 = kron(A,B) % does not work
1 Answers
Related