% PCA demo

% generate points drawn from a Gaussian with some mean and covariance 
mu = [1 5]; 
SIGMA = [.9 .4; .4 .3]; 
X = mvnrnd(mu, SIGMA, 20); 

% perform PCA
[E, U, lambda] = PCA(X, 1);

% plot original points
figure; scatter(X(:, 1), X(:, 2))
title('Original points');

% plot direction of maximum variance as a vector
figure;     % make new figure
plotv(U)
title('Direction of maximum variance');

% plot points on the line of maximum variance
figure;     % make new figure
proj_matrix = U*U';
%m = U(2)/U(1); proj_matrix = 1/(1+m^2) * [1 m; m m^2];   % equivalently
proj = X * proj_matrix;
x_coords = proj(:, 1);
y_coords = proj(:, 2);
scatter(x_coords, y_coords)
title('Points on line of maximum variance');
