% Make a pca projection of a dataset.

% Load the data
[x,xt,nin,nout,xndata] = datread('sattrn.txt');
[y,yt,nin,nout,yndata] = datread('sattst.txt');

% Find the eigenvectors and eigenvalues of the covariance matrix
C = cov(x);  % matlab has a function cov which computes the covariance
             % matrix of the data
[evecs, evals] = eig(C);   % computing eigenvalues/vectors is easy in matlab!

% Sort the eigenvalues to find the two largest
[sev, I] = sort(diag(evals));

% Set up a projection matrix, using the largest two eigenvectors
projmat = zeros(nin,2); % first create a matrix full of zeros
projmat(:,1) = evecs(:,I(nin));
projmat(:,2) = evecs(:,I(nin-1));

% Centre the data and project it onto the first two principal components
n = size(x,1);
xc = x - ones(n,1)*mean(x);
proj = xc*projmat;

% Pick out the points labelled class 1 and plot them as red crosses
plot(proj(xt(:,1)==1,1), proj(xt(:,1)==1,2), 'r+')
hold on

% Plot the other classes in the same way
plot(proj(xt(:,2)==1,1), proj(xt(:,2)==1,2), 'b+')
plot(proj(xt(:,3)==1,1), proj(xt(:,3)==1,2), 'g+')
plot(proj(xt(:,4)==1,1), proj(xt(:,4)==1,2), 'y+')
plot(proj(xt(:,5)==1,1), proj(xt(:,5)==1,2), 'c+')
plot(proj(xt(:,6)==1,1), proj(xt(:,6)==1,2), 'k+')
hold off

