Implementation of SVD Matlab

I tried to write Matlab code that decomposes the matrix into its SVD form.

"Theory":

To get U, I found the eigenvectors AA ', and to get V, I found the eigenvectors 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 does not 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 'seems to be returning with a factor of -1:

ans =

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

What error in code or "theory" led to this?

+4
source share
1 answer

( Av==Ξ»v , w ΞΌw==v ΞΌ~=0 ). , , eig, SVD ( ).

U V, A'*A, . V , U . V , A*V == U*sigma.

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

, A == U*sigma*V', U U, .

+5
source

All Articles