When using eig function in Matlab, it seems that this function has already normalize the values of the eigenvalues. Do we need to write some lines of code to normalize the eigenvalues after using the eig function.
The function eig in MATLAB normalizes the eigenvectors (not the eigenvalues).
See the following from the documentation:
[V,D] = eig(A) returns matrix V, whose columns are the right
eigenvectors of A such that AV = VD. The eigenvectors in V are
normalized so that the 2-norm of each is 1.
Eigenvectors can vary by a scalar, so a computation algorithm has to choose a particular scaled value of an eigenvector to show you. eig chooses 2-norm = 1. Just look at the eigenvector definition to see why: AV=VD. V shows up on both sides, so you can multiple V by anything without affecting the equation.
Eigenvalues do not vary. Look again at AV=VD. D is only on one side, so it can't be scaled.
Related
I know that the eigenvectors produced by eig(A) have 2-norm 1. But what about the vectors produced in the generalized eigenvalue problem eig(A,B)? A natural conjecture is that such a vector v should satisfy v'Bv=1. When B is the identity matrix, then v'Bv is exactly the square of the 2-norm. I ran the following test for various matrices A and B:
[p,d]=eig(A,B);
v=p(:,1);
v'*B*v
I always choose B to be diagonal. I noticed that v'Bv is not always 1. However, it is indeed 1 when A is symmetric. Does anyone know the rule for the way that Matlab normalizes the generalized eigenvectors? I can't find it in the document.
According to the documentation (emphasis mine):
The form and normalization of V depends on the combination of input arguments:
[...]
[V,D] = eig(A,B) and [V,D] = eig(A,B,algorithm) returns V as a matrix whose columns are the generalized right eigenvectors that satisfy A*V = B*V*D. The 2-norm of each eigenvector is not necessarily 1. In this case, D contains the generalized eigenvalues of the pair, (A,B), along the main diagonal.
When eig uses the 'chol' algorithm with symmetric (Hermitian) A and symmetric (Hermitian) positive definite B, it normalizes the eigenvectors in V so that the B-norm of each is 1.
This means that, unless you are using the 'chol' algorithm, V is not normalized.
If I get you correctly, you are looking for a way to generalize a vector then given a vector you can divide it by its norm to obtain a secondary vector whose norm is 1.
If you are looking for the mathematical background, then Eigendecomposition of a matrix contains a good introduction.
When I get the eigenvalues of the diagonal of a PCA transformed image, I always get 1, whatever the image. What's the reason behind this?
I used the following code.
coeff = pca(pmap);
disp(coeff);
[V,L]=eig (coeff'*coeff);
Lamda = diag(L);
disp(Lamda);
The coeff which pca outputs are already eigenvectors, which are all orthogonal. They are even orthonormal, since MATLAB normalises them. Relative weight is in the explained output parameter of pca.
So transpose(coeff)*coeff gives you the identity matrix, which just contains ones and the eigenvectors of the identity matrix are, obviously, all just 1 in a single dimension.
The reason is thus because that's how linear algebra works.
I use the MATLAB to do eigenvalue decomposition, and the dimension of data is about 10000, so the covariance matrix is 10000*10000. When I use the eig() function in MATLAB, it is very slow. Is there any way to speed up the eigenvalue decomposition.
I use the eigenvalue decomposition to do principal component analysis(PCA), so I just use the top K eigenvalues and eigenvectors. There is no need to get all the eigenvalues and eigenvectors. I have tried to use the Intel-MKL to do eigen decomposition, but when I use the mex interface, there are some errors. I posted it in the link https://stackoverflow.com/questions/19220271/how-to-use-intel-mkl-for-speed-my-own-matlab-mex-cpp-applications
Please give me some advice, Thanks.
use eigs if your data is sparse, or if you are interested in the first k values. For example,
eigs(A,k) returns the k largest magnitude eigenvalues. Note that eigs will be faster only for the first few eigen-values, and will be slower for k > some value (probably 5...)
Here is my testing function:
function diff = svdtester()
y = rand(500,20);
[U,S,V] = svd(y);
%{
y = sprand(500,20,.1);
[U,S,V] = svds(y);
%}
diff_mat = y - U*S*V';
diff = mean(abs(diff_mat(:)));
end
There are two very similar parts: one finds the SVD of a random matrix, the other finds the SVD of a random sparse matrix. Regardless of which one you choose to comment (right now the second one is commented-out), we compute the difference between the original matrix and the product of its SVD components and return that average absolute difference.
When using rand/svd, the typical return (mean error) value is around 8.8e-16, basically zero. When using sprand/svds, the typical return values is around 0.07, which is fairly terrible considering the sparse matrix is 90% 0's to start with.
Am I misunderstanding how SVD should work for sparse matrices, or is something wrong with these functions?
Yes, the behavior of svds is a little bit different from svd. According to MATLAB's documentation:
[U,S,V] = svds(A,...) returns three output arguments, and if A is m-by-n:
U is m-by-k with orthonormal columns
S is k-by-k diagonal
V is n-by-k with orthonormal columns
U*S*V' is the closest rank k approximation to A
In fact, usually k will be somethings about 6, so you will get rather "rude" approximation. To get more exact approximation specify k to be min(size(y)):
[U, S, V] = svds(y, min(size(y)))
and you will get error of the same order of magnitude as in case of svd.
P.S. Also, MATLAB's documentations says:
Note svds is best used to find a few singular values of a large, sparse matrix. To find all the singular values of such a matrix, svd(full(A)) will usually perform better than svds(A,min(size(A))).
I am using LAPACK's ssteqr function to calculate eigenvalues/eigenvectors. The documentation for ssteqr says that the eigenvalues are sorted "in ascending order". Is it reasonable to assume that the list of eigenvectors is also sorted in ascending order?
Yes, it is reasonable to assume that the eigenvectors are ordered so that the i-th eigenvector corresponds to the i-th eigenvalue.
Nevertheless, if I were you, I would check for each eigenvalue the result of the multiplication of the eigenvector by the matrix. This way you are sure that you interpret the output right, and you see explicitly the accuracy of the calculations.
An old question, this, but I struggled with this recently, so am adding this for current and future readers.
The basic answer is that, yes, the eigenvectors are sorted such that the ith eigenvector corresponds to the ith eigenvalue. However, note that the eigenvectors thus obtained may not be the actual eigenvectors you want. This is so because of the following.
Since the ?steqr functions work only on tridiagonal matrices, one typically uses LAPACK's ?sytrd functions to first transform one's original symmetric matrix, call it M, to a tridiagonal form, call it T, such that M = QTQT where Q is an orthogonal matrix (and QT denotes its transpose). One then applies the ?steqr function on this tridiagonal matrix T to find its eigenvalues and eigenvectors. Now the eigenvalues thus obtained (of T) are exactly the same as the eigenvalues of M, so if one only wants the eigenvalues one can stop here. But if one is interested in the eigenvectors, like the OP, then one needs to bear in mind that the eigenvectors of T and M are different. To find the eigenvectors of the original matrix M, one needs to left-multiply the obtained eigenvectors of T by Q. This is very easily done by using the LAPACK functions orgtr or ormtr. See here for a clear explanation: https://software.intel.com/en-us/mkl-developer-reference-fortran-sytrd.