Using "for" loop for summation of matrix product

2 Ansichten (letzte 30 Tage)
Nir Vaishnav
Nir Vaishnav am 11 Jul. 2022
Kommentiert: Jeffrey Clark am 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.

Akzeptierte Antwort

Jeffrey Clark
Jeffrey Clark am 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 Kommentare
Nir Vaishnav
Nir Vaishnav am 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
Jeffrey Clark am 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)?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by