Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SVD Matlab Implementation

Tags:

matlab

svd

I tried to write matlab code that would decompose a matrix to its SVD form.

"Theory":

To get U, I found the eigenvectors of AA', and to get V, I found the eigenvectors of A'A. Finally, Sigma is a matrix of the same dimension as A, with the root of the eigenvalues on the diagonal in an ordered sequence.

However, it doesn't seem to work properly.

A=[2 4 1 3; 0 0 2 1];

% Get U, V
[aatVecs, aatVals] = eig(A*A');
[~, aatPermutation] = sort(sum(aatVals), 'descend');
U = aatVecs(:, aatPermutation);

[ataVecs, ataVals] = eig(A'*A);
[~, ataPermutation] = sort(sum(ataVals), 'descend');
V = ataVecs(:, ataPermutation);

% Get Sigma
singularValues = sum(aatVals(:, aatPermutation)).^0.5;
sigma=zeros(size(A));
for i=1:nnz(singularValues)
    sigma(i, i) = singularValues(i);
end

A
U*sigma*V'

U * sigma * V' seem to be returned with a factor of -1:

ans =

-2.0000   -4.0000   -1.0000   -3.0000
0.0000    0.0000   -2.0000   -1.0000

What's the mistake in the code or "theory" that led to it?

like image 633
Ana M Avatar asked Feb 27 '16 02:02

Ana M


People also ask

How does Matlab do SVD?

Description. S = svd( A ) returns the singular values of matrix A in descending order. [ U , S , V ] = svd( A ) performs a singular value decomposition of matrix A , such that A = U*S*V' .

Can SVD be applied to any matrix?

The singular value decomposition is very general in the sense that it can be applied to any m × n matrix, whereas eigenvalue decomposition can only be applied to diagonalizable matrices.


1 Answers

The eigenvectors are not unique (because Av==λv by definition, any w with μw==v and μ~=0 is also an eigenvector). It so happens that the eigenvectors returned by eig don't match up in the right way for SVD (even though they are normalized).

However, we can construct U once we have V, which we will find as eigenvectors of A'*A as in your algorithm. Once you have found V as sorted eigenvectors, you have to find U to match. Since V is orthogonal, A*V == U*sigma. So we can set

U = [A*V(:,1)./singularValues(1) A*V(:,2)./singularValues(2)];

and indeed, A == U*sigma*V', and in particular the U found here is exactly the negative of U found with your algorithm.

like image 85
zeeMonkeez Avatar answered Oct 14 '22 04:10

zeeMonkeez