function CS6640_Week10_PCA
%

% 2D data
x = [-5:5];
y = [-5:5];
n = length(x);
pts = [x',y'];
figure(1)
clf
plot(x,y,'k.');
hold on
plot(-6,-6,'w.');
plot(6,6,'w.');

% Covariance matrix
C1 = cov(pts)
C2 = pts'*pts/(n-1)

% Covariance matrix is a map
% C: pts --> pts
% consider the affect on a circle
t = [0:0.01:2*pi];
cx = cos(t);
cy = sin(t);
figure(2);
clf
plot(cx,cy,'k');
axis equal
hold on
axis equal
cpts = [cx',cy'];
dcpts = cpts;
cptst = (C1*cpts')';
dcptst = cptst;
plot(cptst(:,1),cptst(:,2),'r');

% Try some more
C = [30,0;0,30];
clf
ptst = (C*cpts')';
plot(ptst(:,1),ptst(:,2),'k');
axis equal

C = [10,0;0,20];
clf
ptst = (C*cpts')';
plot(ptst(:,1),ptst(:,2),'k');
axis equal

C = [20,0;0,10];
clf
ptst = (C*cpts')';
plot(ptst(:,1),ptst(:,2),'k');
axis equal

% Zero mean points
pts = 10*rand(20,2);
xm = mean(pts(:,1));
ym = mean(pts(:,2));
pts0 = pts;
pts0(:,1) = pts0(:,1) - xm;
pts0(:,2) = pts0(:,2) - ym;

figure(1);
clf
hold on
plot(pts(:,1),pts(:,2),'ko');
plot(pts0(:,1),pts0(:,2),'r*');
plot([-10,10],[0,0],'k');
plot([0,0],[-10,10],'k');

% Demonstration of C*v = lambda*v
CS6640_PCA_angles(cpts,cptst);
[V,D] = eigs(C1);
princ_axis1 = posori(atan2(V(2,1),V(1,1)))*180/pi
princ_axis2 = posori(atan2(V(2,2),V(1,2)))*180/pi

%[U,S,V] = svd(C)

tch = 0;
