开发者

Eigenvalues in MATLAB

In MATLAB, when I run the command [V,D] = eig(a) for a symmetric matrix, the largest eigenvalue (and its associated vector) is located in last column. However, when I run it with a non-symmetric matrix, the largest eigenvalue is in the 开发者_如何学运维first column.

I am trying to calculate eigenvector centrality which requires that I take the compute the eigenvector associated with the largest eigenvalue. So the fact that the largest eigenvalue appears in two separate places it makes it difficult for me to find the solution.


What I usually do is:

[V D] = eig(a);
[D order] = sort(diag(D),'descend');  %# sort eigenvalues in descending order
V = V(:,order);


You just have to find the index of the largest eigenvalue in D, which can easily be done using the function DIAG to extract the main diagonal and the function MAX to get the maximum eigenvalue and the index where it occurs:

[V,D] = eig(a);
[maxValue,index] = max(diag(D));  %# The maximum eigenvalue and its index
maxVector = V(:,index);           %# The associated eigenvector in V

NOTE: As woodchips points out, you can have complex eigenvalues for non-symmetric matrices. When operating on a complex input X, the MAX function uses the magnitude of the complex number max(abs(X)). In the case of equal magnitude elements, the phase angle max(angle(X)) is used.


Note that non-symmetric matrices tend to have complex eigenvalues.

eig(rand(7))
ans =
       3.2957              
     -0.22966 +    0.58374i
     -0.22966 -    0.58374i
     -0.38576              
      0.49064              
      0.17144 +    0.27968i
      0.17144 -    0.27968i

Also note that eig does not explicitly return sorted eigenvalues (although the underlying algorithm tends to produce them in a nearly sorted order, based on the magnitude of the eigenvalue), but even if you do do a sort, you need to understand how sort works on complex vectors.

sort(rand(5,1) + i*rand(5,1))
ans =
      0.42343 +    0.51539i
    0.0098208 +    0.76145i
      0.20348 +    0.88695i
      0.43595 +    0.83893i
       0.8225 +    0.91264i

Sort, when applied to complex inputs, works on the magnitude of the complex number.


If you only care for the eigenvector associated with the largest eigenvalue, isn't it better to use eigs?

[V, D] = eigs( a, 1, 'lm' ); %// get first eigenvector with largest eigenvalue magnitude.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜