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

  1. function plscrs = plscross(a, c, maxrank)
  2. % PLSCROSS    Calculate CROSS as a function of the number of factors used
  3. % Copyright (c) 1989-92 by The MathWorks, Inc.
  4. %
  5. %  plscrs = plscross(a, c, maxrank)
  6. %
  7. % Where:
  8. %
  9. %  plscrs    is the matrix containing cross
  10. %  a         is the training set absorbance matrix
  11. %  c         is the training set concentration matrix
  12. %  maxrank   is optional limit to the number of factors tested
  13. %
  14.  
  15. [i, j] = size(a);
  16.  
  17. if nargin == 3, i = maxrank; end
  18.  
  19. plscrs(i,1)=0;
  20. for n = 1:j
  21.  
  22. x = a(:, [1:(n-1), (n+1):j])';
  23. y = c(:, [1:(n-1), (n+1):j])';
  24.  
  25. k = 0;
  26.  
  27. for h = 1:i,
  28.     u(:,h) = y(:,1);
  29.     told(:,h) = zeros(j-1,1);
  30.     t(:,h) = ones(j-1,1);
  31.     while (sum(abs(told(:,h)-t(:,h)))) > (1e-10) & k < 100
  32.         told(:,h) = t(:,h);
  33.         k = k + 1;
  34.         w(h,:)  = u(:,h)' * x / (u(:,h)' * u(:,h));
  35.         w(h,:)  =  w(h,:) / sqrt(sum(w(h,:) .* w(h,:)));
  36.         t(:,h)  = x * w(h,:)' / (w(h,:) * w(h,:)');
  37.         q(h,:) = t(:,h)' * y / (t(:,h)' * t(:,h));
  38.         u(:,h)  = y * q(h,:)'/ (q(h,:) * q(h,:)');
  39.     end
  40.         l(h) = k;
  41.     k = 0;
  42. p(h,:) = t(:,h)' * x / (t(:,h)' * t(:,h));
  43. p(h,:) = p(h,:) / sqrt(sum(p(h,:) .* p(h,:)));
  44. t(:,h)  = t(:,h) *  sqrt(sum(p(h,:) .* p(h,:)));
  45. w(h,:)  = w(h,:) *  sqrt(sum(p(h,:) .* p(h,:)));
  46. b(h) = (u(:,h)' * t(:,h)) / (t(:,h)' * t(:,h));
  47. x = x - (t(:,h) * p(h,:));
  48. y = y - (b(h) * t(:,h) * q(h,:));
  49. end
  50. for f = 1:i
  51.  
  52.     x = a(:,n)';
  53.     cr = 0;
  54.     for h=1:f
  55.         tr(:,h) = x * w(h,:)';
  56.         x = x - tr(:,h) * p(h,:);
  57.         cr = cr + b(h) * tr(:,h) * q(h,:);
  58.     end
  59.     cr = cr';
  60.     error = cr - c(:,n);
  61.        plscrs(f, 1) = plscrs(f, 1) + sum(sum(error .* error));
  62.  
  63. end
  64. end
  65.