Why does kron(D,I), with D and I both sized (nxn), not create a matrix sized (n^2 x n^2)?

1 view (last 30 days)
I am trying to run a code that involves operating on the elements of a matrix made by a kronecker sum operation, but I meet a sub2ind error. I have found that the problem is that MATLAB doesn't generate the matrix size it is supposed to generate after one kind of kronecker product operation in particular:
x = sym('x', [1 15], 'real')
x = 
I = eye(5)
I = 5×5
1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1
D = diag(-x(1) -x(2) -x(3) -x(4) -x(5))
D = 
disp(kron(I,D))
Which causes an error in my code that involves the following:
DsumD = kron(D,I) + kron(I,D)
DsumD = 
zeta = sym(zeros(1,25));
for i = 1:25
zeta(1,i) = -DsumD(i,i);
end
Error using sub2ind (line 43)
Out of range subscript.

Error in sym/subsref (line 992)
R_tilde = sub2ind(size(L), Idx.subs{:});
zetatopi = sym(zeros(1,25));
for j = 1:25
zetatopi(1,i) = log(1+zeta(1,i)*x(15))/x(15);
end
DsumDtopi = diag(-zetatopi)
Does anyone know how I can make MATLAB do the kronecker operations properly?

Accepted Answer

Paul
Paul on 14 Dec 2021
Because D is not 5 x 5
x = sym('x', [1 15], 'real');
D = diag(-x(1) -x(2) -x(3) -x(4) -x(5))
D = 
size(D)
ans = 1×2
1 1
Perhaps you meant
D = diag(x(1:5))
D = 
or
D = diag([-x(1) -x(2) -x(3) -x(4) -x(5)])
D = 

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!