k = 30; % adjust to choose your rank of the approximation
A = imread('baboon.png');
A = double(A);

figure; % plot original image
imshow(uint8(A)) 
axis off
axis equal


Ak = zeros(size(A));
for i = 1:3
    [U,S,V] = svd(A(:,:,i)); % perform SVD
    Ak(:,:,i) = U(:,1:k)*S(1:k,1:k)*V(:,1:k)'; % select k rank approximation
end

figure; % plot approximation
imshow(uint8(Ak)); 
axis off
axis equal


