function ShowSVDCompress()
close all
% Read in the image as a black and white jpeg...
A = imread('Clock.jpg');
imshow(A)
title('Original Imag','fontsize',16)
shg
pause(1)
% Compute and plot its singular values (normalized by the largest singular
% value...
A = double(A);
[m,n] = size(A);
[U,S,V] = svd(A);
figure
semilogy(diag(S))
title('The Singular Values of A','fontsize',18)
shg
pause

for r=20:20:140
    % Compute the closest rank-r matrix to A and display...
    A_r = LowRank(A,r);
    figure
    imshow(uint8(A_r))
    sizeOfCompressedRepresentation = r*(m+n);
    sizeOfOriginalImage = m*n;
    CF = sizeOfOriginalImage/sizeOfCompressedRepresentation;
    title(sprintf('rank = %2d    Compression Factor = %5.2f ',r,CF),'fontsize',16)
    pause(1)
end

function A_r = LowRank(A,r)
[m,n] = size(A);
[U,S,V] =svd(A);
A_r = zeros(m,n);
for k=1:r
    A_r = A_r + S(k,k)*U(:,k)*V(:,k)';
end 
    
