SVD computation using eig function
Show older comments
I'm trying to find the Singular Value Decomposition of a rectangular matrix by calculating the eigenvalues and eigenvectors of
and $A^TA$. Here's what I have
clear all; close all; clc;
% Control rng
rng('default')
s = rng
A = randi([0, 20], [7,4]); % generate random 7x4 matrix A
[U1,S1,V1] = svd(A); % Matlab's svd
% My svd
[U_tilde,L1] = eig(A*A');
[V_tilde,L2] = eig(A'*A);
% Sort eigenvalues and the corresponding eigenvectors
[D1,ind1] = sort(diag(L1),'descend');
U_tilde = U_tilde(:,ind1);
[D2,ind2] = sort(diag(L2),'descend');
V_tilde = V_tilde(:,ind2);
D = L2(ind2,ind2);
[m,n]=size(A);
L = zeros(m,n);
L(1:n,1:n) = D;
But when I compute (U_tilde)*(L.^0.5)*(V_tilde') I don't get the original matrix A.
>> U_tilde*L.^0.5*V_tilde'
ans =
0.7203 13.7281 5.4162 21.1586
4.8700 13.3098 26.8486 11.5016
19.3466 9.7884 17.7988 6.4885
2.5526 27.0760 7.0479 11.7346
13.7950 17.3916 16.3837 16.9942
22.6812 14.4418 9.8290 14.4702
10.1597 11.1548 5.3215 10.1012
>> A
A =
17 11 16 0
19 20 2 17
2 20 8 19
19 3 19 14
13 20 16 15
2 20 20 15
5 10 13 8
V1 and V_tilde are equal up to the sign, U1 and U_tilde are equal up to the 4-th column. S1 and L.^0.5 are equal. What's wrong with my code?
Accepted Answer
More Answers (0)
Categories
Find more on Linear Algebra in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!