function [U,Xmean,D,V,Cov]=f(X);
%
% Syntax:  [U, Xmean, D, V, Cov] = klt(X);
%
% This functions takes an N-by-M matrix of M N-dimensional observations
% and computes the eigenvalue-ranked eigenvectors (principal components)
%
% It returns the eigenvectors in U, the mean vector in Xmean.
% D is a vector of eigenvalues. V is the eigenvectors of the rank-M
% covariance matrix Cov
%
% NOTE: This version is optimized for N >> M

% ----------------------------------------------------------------------
% Author: baback@media.mit.edu
% Date  : 06/14/93

[N,M]=size(X);
Xmean = mean(X')';
P = X - kron(ones(1,M),Xmean);
Cov = P'*P/M;
[Vtemp,D]=eig(Cov);
eigvals = diag(D);
[etemp,I]=sort(eigvals);
V=Vtemp;
eigvals=flipud(etemp);

for i=1:M
	V(:,i)=Vtemp(:,I(M+1-i));
end;
D = eigvals;

U = P*V;
for i=1:M-1;
 	U(:,i)=U(:,i)/sqrt(sum(U(:,i).^2));
end;
U(:,M)=zeros(N,1);


