I am working on MATLAB problems from my textbook and one of the problems asks me to use the eig
command in MATLAB, compute the matrices V
and D
such that A = V * D * inv(V)
. Knowing that the first column of V
corresponds to the first eigenvalue D(1,1)
and so on, I need to reorder the diagonal entries of D
so that the real part is increasing down the diagonal and reorder the columns of V
accordingly so that A = V * D * inv(V)
still holds. Here's what I have written so far:
r = RandStream('mt19937ar','Seed',1234);
A = r.randn(10,10)+1j*r.randn(10,10);
[V,D] = eig(A);
for tt = 1:9
if (real(D(tt,tt)) > real(D(tt+1,tt+1)))
temp = D(tt,tt);
D(tt,tt) = D(tt+1,tt+1);
D(tt+1,tt+1) = temp;
tempV = V(1,tt);
V(1,tt) = V(1,tt+1);
V(1,tt+1) = tempV;
if (A == V*D*inv(V))
break
end
end
end
When I tested it, the diagonal elements of D
did not change from the original order, I know it might be due to the conditionals I set, but I am not sure what specifically is causing it to not do anything. I also think there might be issues in the way I am reordering the diagonal elements and corresponding eigenvectors. Any feedback or suggestions is appreciated, thank you in advance.