How to sort the matrix quickly?

조회 수: 2 (최근 30일)
Xianjie
Xianjie 2012년 12월 10일
Dear all, How to sort the matrix quickly? Now i use this code to do it.
[V,DD]=eig(KK,MM);
DD=abs(DD);
mp=3*(Ms+1)*(2*Ns+1)+P1*(2*Ns+1)+2*P2*(2*Ns+1); % the total dimension of the matrix
for p=1:mp
for q=p+1:mp
if DD(p,p)>= DD(q,q)
d=DD(p,p);
dd=V(:,p);
DD(p,p)=DD(q,q);
V(:,p)=V(:,q);
DD(q,q)=d;
V(:,q)=dd;
end
end
end
Thank you very much.

답변 (2개)

Image Analyst
Image Analyst 2012년 12월 10일
And what's wrong with the built-in sort() function? Why aren't you using that? Is this a homework exercise?
  댓글 수: 2
Xianjie
Xianjie 2012년 12월 10일
Hello, the higher and lower can find in the D matrix. The V is corresponding to the D each column. I compare the value of D, can move the D and V in the same. sort only can sort the D? the D is the diag matrix
Image Analyst
Image Analyst 2012년 12월 10일
Uh, not quite sure I follow that. Anyway, Roger did it below using sort() and he's highly respected and always or nearly always right, so I believe using the built-in sort() function is the way to go.

댓글을 달려면 로그인하십시오.


Roger Stafford
Roger Stafford 2012년 12월 10일
The following should be a much faster way of sorting V and DD by the absolute values of the eigenvalues in DD:
[V,DD] = eig(KK,MM);
D = diag(DD);
[~,p] = sort(abs(D));
V = V(:,p);
DD = diag(D(p));
Roger Stafford
  댓글 수: 1
Xianjie
Xianjie 2012년 12월 10일
Thank you for your advice. Can you guid me for the usage of lapack for the eigenvalue problem. This is the lapack. But i am the first time to use the lapack. How to use this script to solve the eigenvalue problem? if the matrices is large, like 4000*4000,it will take a long time to solve this problem. Thank you very much.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by