function [vc, vl] = pca(a, maxrank)
% PCA  Calculate eigensystem of covariance of absorbance matrix.
%
%	PCA  Calculate eigenvectors and eigenvalues
%       of the covariance matrix of an absorbance matrix.
%
%  [vc, vl] = pca(a, maxrank)
%
% Where:
%
%  vc       is the matrix containing the eigenvectors (factors)
%  vl       is the column vector containing the eigenvalues
%  a        is the absorbance matrix
%  maxrank  is optional limit to the number of factors tested
%

% Copyright (c) 1989-92 by The MathWorks, Inc.

resid = a * a';
[i,j]=size(a);
vc = zeros(i,i);
vcold = vc(:,1);
vl(i,1) = 0;
ii = i;
if nargin == 2, i = maxrank; end
for n=1:i,
	k = 0;
	vc(:,n) = vc(:,n) + sqrt(1/ii);
	while sum(abs(vc(:,n)-vcold)) > sum(abs(vcold) * 1e-10) & k < 100;
		k = k + 1;
		vcold = vc(:,n);
		vc(:,n) =  resid * vcold;
		vl(n,1) = sqrt(sum(vc(:,n) .* vc(:,n)));
		vc(:,n) = vc(:,n) / vl(n);
	end
	resid = resid - vl(n) * vc(:,n) * vc(:,n)';
end
