home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l455 / 6.ddi / CHEM.DI$ / PCA.M < prev    next >
Encoding:
Text File  |  1993-03-11  |  988 b   |  38 lines

  1. function [vc, vl] = pca(a, maxrank)
  2. % PCA  Calculate eigensystem of covariance of absorbance matrix.
  3. %
  4. %    PCA  Calculate eigenvectors and eigenvalues
  5. %       of the covariance matrix of an absorbance matrix.
  6. %
  7. %  [vc, vl] = pca(a, maxrank)
  8. %
  9. % Where:
  10. %
  11. %  vc       is the matrix containing the eigenvectors (factors)
  12. %  vl       is the column vector containing the eigenvalues
  13. %  a        is the absorbance matrix
  14. %  maxrank  is optional limit to the number of factors tested
  15. %
  16.  
  17. % Copyright (c) 1989-92 by The MathWorks, Inc.
  18.  
  19. resid = a * a';
  20. [i,j]=size(a);
  21. vc = zeros(i,i);
  22. vcold = vc(:,1);
  23. vl(i,1) = 0;
  24. ii = i;
  25. if nargin == 2, i = maxrank; end
  26. for n=1:i,
  27.     k = 0;
  28.     vc(:,n) = vc(:,n) + sqrt(1/ii);
  29.     while sum(abs(vc(:,n)-vcold)) > sum(abs(vcold) * 1e-10) & k < 100;
  30.         k = k + 1;
  31.         vcold = vc(:,n);
  32.         vc(:,n) =  resid * vcold;
  33.         vl(n,1) = sqrt(sum(vc(:,n) .* vc(:,n)));
  34.         vc(:,n) = vc(:,n) / vl(n);
  35.     end
  36.     resid = resid - vl(n) * vc(:,n) * vc(:,n)';
  37. end
  38.