Eigenvalues in MATLAB
Asked Answered
M

4

9

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.

Mcclure answered 20/7, 2010 at 15:43 Comment(1)
Is it that hard to explicitly search through the array of eigenvalues and grab the largest one?Noetic
W
5

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.

Wolgast answered 20/7, 2010 at 15:51 Comment(0)
D
16

What I usually do is:

[V D] = eig(a);
[D order] = sort(diag(D),'descend');  %# sort eigenvalues in descending order
V = V(:,order);
Demonetize answered 20/7, 2010 at 17:46 Comment(2)
This is a more general solution that is useful when computing PCA.Pb
how to do this in eigs?Superdreadnought
W
5

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.

Wolgast answered 20/7, 2010 at 15:51 Comment(0)
K
4

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.

Krein answered 20/7, 2010 at 19:58 Comment(1)
+1 hadn't thought of complex numbers. I guess I usually only care about the real eigenvaluesDemonetize
C
1

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.
Counterwork answered 27/11, 2014 at 13:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.