这是从Gonzalez的书上抄的
function P=pca(X,q)
%PCA Obtains principal component vectors and realted
quantities.
% P=PCA(X,Q) computes the principal component vectors of the
vector
% population contained in the rows of X, a matrix of size K-by-n
where K is
% the number of vectors and n is their dimonsionality. Q ,with
values in the
% range [0,n], is the number of eigenvectors used in constructing
the
% principal components transformation matrix. P is a structure with
the
% following fields:
% P.Y K-by-Q matrix whose columns are the principal component
% vectors.
% P.A Q-by-n principal components transformation matrix whose rows
% are the Q eigenvectors of Cx corresponding to the Q largest
% eigenvalues.
% P.X K-by-n matrix whose rows are the vectors reconstructed from
% the principal component vectors. P.X and P.Y are identical if
Q=n.
% P.ems The mean square error
incurred in using only the Q
% eigenvectors corresponding to the largest eigenvalues. P.ems is 0
if
% Q=n.
% P.Cx The
n-by-n convariance matrix of the population in X.
% P.mx The
n-by-1mean vector of the population in X.
% P.Cy The
Q-by-Q covariance matrix of the population in Y. The
% main diagonal contains the eigenvalues ( in descending order)
% corresponding to the Q eigenvectors.
% Copyright by Rafael C. Gonzalez,.
% This is a copy from his book.
[K,n]=size(X);
X=double(X);
%Obtain the mean vector and covariance matrix of the vectors in
X.
[P.Cx,P.mx]=covmatrix(X);
P.mx=P.mx';% Convert mean vector to a row vector
%Obtain the eigenvectors and corresponding eigenvaluse of Cx.
The
%eigenvectors are the columns of n-by-n matrix V. D is an n-by-n
diagnoal
%matrix whose elements along the main diagoanl are the
eigenvaluse
%corresponding to the eigenvectors in V, so that X*V =D*V.
[V,D]=eig(P.Cx);
%Sort the eigenvalues in decreadsing order. Rearrange the
eigenvectors to
%match.
d=diag(D);%Subtract the diagonal elements of
D.
[d,idx]=sort(d);
d=flipud(d);%d is in decreasing order now.
idx=flipud(idx);
V=V(:,idx);%Rearrage the order of eigenvectors as d.
%Now form the q rows of A from first q columns of V.
P.A=V(:,1:q)';
%Compute the principal component vectors.
Mx=repmat(P.mx,K,1);%M-by-n matrix.Each row=P.mx.
P.Y=P.A*(X-Mx)';% q-by-k matrix.
%Obtain the reconstructed vectors.
P.X=(P.A'*P.Y)'+Mx;
%Convert P.Y to K-by-q array and P.mx to n-by-1 vector.
P.Y=P.Y';
P.mx=P.mx';
%The mean square error is given by the sum of all the
eigenvalues minus the
%sum of the q largest eigenvalues.
P.ems=sum(d(q+1:end));
%Covariance matrix of the Y's.
P.Cy=P.A*P.Cx*P.A';