A = [];
An = [];

for i =1:20
    for j = 1:20
        A = [A;[i,j,j/2]];
    end
end
Sigma = cov(A);
[W,E] = eigs(Sigma)


figure;
scatter3(A(:,1),A(:,2),A(:,3),'r')
hold on;
line([0,-W(1,1)*10],[0,-W(2,1)*10],[0,-W(3,1)*10])
line([0,W(1,2)*10],[0,W(2,2)*10],[0,W(3,2)*10])




for i =1:20
    for j = 1:20
        An = [An;[i,j,j/2 + randn(1,1)]];
    end
end

Sigman = cov(An);
[W,E] = eigs(Sigman)

Ah = (An-repmat(mean(An),size(An,1),1))*W(:,1:2)*W(:,1:2)' + repmat(mean(An),size(An,1),1);

figure;
scatter3(An(:,1),An(:,2),An(:,3),'r')
hold on;
scatter3(Ah(:,1),Ah(:,2),Ah(:,3),'k')