I'm trying to create code that finds the svd of an image. We're not allowed to actually use the svd() function. I managed to get an answer that's close but not exact. I'm comparing my answers to the result of the SVD function and my middle matrix is perfect but some of the signs on my left and right matrices are flipped. Also if I try and display the image again using my SVD it looks like a low rank approximation of the original picture.
Basically what I'm doing is using eig() to take the eigenvalues and eigenvectors of AAtranspose and AtransposeA and then reordering them from largest to smallest. As far as I understand eig() puts them in order from smallest to largest so I just used the fliplr function to swap the order.
I'm not terribly experienced with Matlab so I might be making a silly mistake. I tried removing the fliplr function but I'm still getting what appears to be the exact same eigenvectors and the image is still coming out wrong. Here is the code I'm using. Is there another error I'm making?
A = imread('picture.jpg');
A2 = double(rgb2gray(A));
AT = transpose(A2);
sz = size(A2);
m = sz(1);
n = sz(2);
AAT = A2*AT;
ATA = AT*A2;
evalues = eig(AAT);
evalues = sort(evalues,'descend');
S = diag(evalues);
S(m,n) = 0;
S=S.^(.5);
[v1,e1]=eig(AAT);
e1 = diag(e1);
[~, ind] = sort(abs(e1), 'descend');
v1 = v1(:, ind);
[v2,e2]=eig(ATA);
e2 = diag(e2);
[~, ind] = sort(abs(e2), 'descend');
v2 = v2(:, ind);
final = v1*S*transpose(v2);
final = uint8(final);
imshow(final);