Break off the bottom part of the file and save it under
the name "displaycomponents.m"

__________________________

echo on
%This program demonstrates how singular value
%decomposition can be used to compress an image.
%IThe image trees comes with matlab itself as a demo.

load trees %load the image

a = ind2gray(X,map);%convert the image to gray

[m,n] = size(a);%size of the image

[u,s,v] = svd(a);

sv = diag(s);%singular values of a

figure(1)
plot(sv)
axis([0 50 0 (1+round(max(sv)/10))*10])
xlabel('component')
ylabel('singular value')
title('Decrease in Singular Value with Component')

pause

figure(2)
%The next part generates a slider which allows
%you to control the number of singular values
%that you use in reconstructing the picture.

components = uicontrol('Style','Slider',...
	'Min',1,'Max',100,'Value',20,...
	'Position',[100,20,800,20],...
	'Callback','displaycomponents');


_____________________________________________________



%this is a matlab function which generates and
%draws the image with the slider-controlled
%number of components:

ncomp = get(components,'Value');
ncomp = round(ncomp)

i = 1:ncomp;%select first ncomp values

us = u(:,i);
vs = v(:,i);
ss = diag(sv(i));

%This produces an estimate of a by using only
%the first ncomp singular values.
as = us*ss*vs';


subplot(1,2,1);
imshow(a);
title('Original Image')

subplot(1,2,2);
imshow(as);
title(['Image with ',num2str(ncomp),' Components'])