%
% Script file: PCA.m
% Find the first K Principal Components
%
function Y=PCA(X,K)
[n,d]=size(X);
C=cov(X);
[U D]=eig(C);
L=diag(D);
[sorted index]=sort(L);
Xproj=zeros(K,d);        % initiate a projection matrix
for i=1:K
   Xproj(i,:)=U(:,index(d-i+1))';
end
Y=(Xproj*X')';           % first K principal components
