# 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)
Rex Mantiri on 13 Dec 2021
Commented: Rex Mantiri on 14 Dec 2021
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?

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 =
##### 2 CommentsShowHide 1 older comment
Rex Mantiri on 14 Dec 2021
@Paul I have another question thread for code used in the same research. Here's the link: https://www.mathworks.com/matlabcentral/answers/1608610-how-do-i-estimate-15-parameters-with-the-least-square-method-for-a-non-linear-objective-function-ma. Any input you have on the matter is greatly appreciated.

R2021a

### Community Treasure Hunt

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

Start Hunting!