%
% A simple example for Principal Components Analysis 
% for lossy data compression 
%

% Data 
% height, weight -- Moler's book
%
X = [
47 15
93 35
53 15
45 10
67 27
42 10
]

% SVD 
[U,S,V] = svd(X,0);

% Decode matrix D = V(:,1) 

% PCA reconstruction  
%   Xpca = (X*V(:,1))*V(:,1)' = sigma(1)*U(:,1)*V(:,1)';

Xpca = (X*V(:,1))*V(:,1)'  

%----------------------------------

figure(1)
subplot(2,1,1)
bar([X(:,1) Xpca(:,1)])
legend('data','pca')
title('Height')
subplot(2,1,2)
bar([X(:,2) Xpca(:,2)])
legend('data','pca')
title('Weight')
