% test pca

clear all

load twosfours;
X = data';
% X has 30 columns corresponding to 30 16x16 images.
% The first 20 columns correspond to images of 2s and the last
% 10 columns to images of 4's


[d N] = size(X);

% Compute mean of each row of X and center data by substracting each row
% mean
XMean = mean(X')';  % vector of size d*1, compute mean for each row
Xc=X-repmat(XMean,1,N);

% find eigendecomposition
[W D]=eig(Xc*Xc'/N);  % compute Xc*Xc' of dimension d*d

% compute principal components associated to the two largest eigenvectors
WReduced=W(:,(d-1):d);
pc=WReduced'*Xc;

figure(1)
% Plot principal components.
clf;
plot(pc(1,1:20),pc(2,1:20),'or',pc(1,21:30),pc(2,21:30),'*b');
grid;
print -djpeg90 pcavisual1

% Plot images on a background of size 120 by 250.
I= zeros(200,250);
% I is the background image on which small images are placed.

% now we scale the components to be integers for indexing:
pc(1,:)=pc(1,:)+6*ones(1,N);
pc(2,:)=pc(2,:)+9*ones(1,N);
pc = round(10*pc);

% now we add the individual images to the background image:
for i=1:N
    I(pc(1,i):pc(1,i)+15,pc(2,i):pc(2,i)+15) = reshape(X(:,i),16,16)';
end

figure(2)
% Visualization:
clf;
colormap('gray')
imagesc(I)
print -djpeg pcavisual2


