clear clf %% % We make an image from some text, then reload it as a matrix. % tobj = text(0,0,'Hello world','fontsize',44); ex = get(tobj,'extent'); axis([ex(1) ex(1)+ex(3) ex(2) ex(2)+ex(4)]), axis off saveas(gcf,'hello.png') A = imread('hello.png'); A = double(rgb2gray(A)); figure(1) imagesc(A) colormap gray [m, n] = size(A) %% % Next we show that the singular values decrease exponentially, until they % reach zero (more precisely, are about (sigma_1 * macheps$). [U, S, V] = svd(A); sigma = diag(S); np = 100; figure(2) semilogy(sigma(1:np), 'r.') title('singular values') xlabel('i') ylabel('\sigma_i') grid on %% % The rapid decrease suggests that we can get fairly good low-rank % approximations. figure(3) for i = 1:4 subplot(2, 2, i) k = 2*i; Ak = U(:,1:k)*S(1:k,1:k)*V(:,1:k)'; imshow(Ak, [0 255]) title(sprintf('rank = %d',k)) end %% % Consider how little data is needed to reconstruct these images. For rank % 8, for instance, we have 8 left and right singular vectors plus 8 % singular values, for a compression ratio of better than 25:1. compression = 8*(m+n+1) / (m*n)