# Using "for" loop for summation of matrix product

2 views (last 30 days)
Nir Vaishnav on 11 Jul 2022
Commented: Jeffrey Clark on 11 Jul 2022
I am using nested for loops to multiply matrices and do the summation of the same. But for Lk(k) variable its only taking the kth element instead of whole matrix Lk,
This is the code
Lk=kron(I,bk);
Gk=Hk;
Lkh=transpose(Lk);
Gkh=(Gk)';
B=zeros(M,M);
for k=1:M
for i=1:M
s=0;
for m=1:M
s= s+ inv(Lk(k)'*Gk(k,m)'*(Gk(k,m)*Lk(k))) * Lk(k)'*Gk(k,m)'*Gk(i,m)*Lk(i);
end
B(k,i)=s;
end
end
This is the warning given: Warning: Matrix is singular to working precision.

Jeffrey Clark on 11 Jul 2022
@Nir Vaishnav, you don't say what any of the iputs are, but since kron will produce a matrix (unless both inputs I and bk are either column or row vectors), the indexed Lk(k) and Lk(i) are linear indexs to the a single element. If as you say you want the whole matrix Lk you should loose the subscripts or change them to appropriate row and col ranges.
##### 2 CommentsShow NoneHide None
Nir Vaishnav on 11 Jul 2022
Lk takes the input of identity matrix whose kronecker product is ones matrix so Lk has dimension M×M and Gk is randomly generated matrix having dimensions k×M
Jeffrey Clark on 11 Jul 2022
@Nir Vaishnav, so Lk is a matrix, why do you take only one linerally addressed value from it (the indexed Lk(k) and Lk(i)) and expect a matrix when you are also only multiplying by single values from Gk(k,m) and Gk(i,m)?

### Categories

Find more on Logical in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!