Python eigenvectors
Asked Answered
S

2

8
eigenvalues, eigenvectors = linalg.eig(K)

How can I print just eigenvectors of len(K). So if there is K, 2x2 matrix, I get 4 eigenvectors, how can I print just 2 of them if there is len(K)=2....

Many thanks

Swanskin answered 10/5, 2011 at 16:32 Comment(9)
why not print eigenvectors[:len(K)] ?Pugliese
This matrix K is not always 2x2....it's random 4x4,....Swanskin
Also I can add, print eigenvectors[:len(K)], this will not work if there is just 2x2 matrix, it print's 4 elements, not just 2....Swanskin
@thaking: Four vectors? You are probably getting two vectors each of length two (four numbers in total). Can you show the exact matrix you're passing to eig and the exact results you're getting back.Garmon
Yes 2 vectors length two; 4 numbers TOTAL; I only need on vector, if there is K 2x2;Swanskin
how can you get 4 eigenvectors from 2x2 matrix???Personalism
@Swanskin Slow down cowboy. You don't understand eigenvectors. Head back to your maths text books before proceeding.Windhoek
Ok, it seems like I didn't get it. I thought you should write print eigenvectors[: eigenvalues.index(<index of first almost zero eigenvalue>)]Pugliese
I misunderstood something in my code, so i was little confused,sorry, I solved my problem now.Swanskin
G
12

You are getting two vectors of length two, not four vectors. For example:

In [1]: import numpy as np

In [2]: K=np.random.normal(size=(2,2))

In [3]: eigenvalues, eigenvectors = np.linalg.eig(K)

In [4]: eigenvectors
Out[4]: 
array([[ 0.83022467+0.j        ,  0.83022467+0.j        ],
       [ 0.09133956+0.54989461j,  0.09133956-0.54989461j]])

In [5]: eigenvectors.shape
Out[5]: (2, 2)

The first vector is eigenvectors[:,0], the second is eigenvectors[:,1].

Garmon answered 10/5, 2011 at 16:42 Comment(3)
Yes, but how to print just 1 vector ?Swanskin
Thanks I misunderstood something in my code, so i was little confused, thanks.Swanskin
eigenvectors are columns, not rows;Personalism
P
0

From the manual:

The normalized eigenvector corresponding to the eigenvalue w[i] is the column v[:,i].

Personalism answered 10/5, 2011 at 16:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.