clear
close all

%read in the image and do SVD
B=imread('dalai_lama.jpg');
A=rgb2gray(B);
[U,S,V]=svd(double(A));
s=diag(S); 

%choose how many terms we want to keep of this SVD
n = 10;
Ur = U(:, 1:n);
Vr = V(:, 1:n);
sr = s(1:n);
Sr = diag(sr);
Ar = Ur*Sr*Vr';

%Plot the singular values
figure
plot(s,'linewidth',3)
axis tight
title('Singular values')
grid

%Compare reduced and original images
figure
subplot(1,2,1)
imshow(mat2gray(Ar))
title(['n=' num2str(n)])
subplot(1,2,2)
imshow(mat2gray(A))
title(['n=' num2str(length(s)) ' (full)'])
