function plsprs = plspress(av, cv, p, q, w, b, rank)
% PLSPRESS    Calculate PRESS as a function of the number of factors used
%
% Copyright (c) 1989-92 by The MathWorks, Inc.
%
%  plsprs = plspress(av, cv, p, q, w, b, rank)
%
% Where:
%
%  plsprs is the matrix containing press
%  av     is the validation set absorbance matrix
%  cv     is the validation set concentration matrix
%  p      is the matrix of PLS spectral factors
%  q      is the matrix of PLS concentration factors
%  w      is the matrix of PLS weights
%  b      is the vector containing the PLS inner relationships
%  rank   is optional limit to the number of factors tested
%
[i, j] = size(p);
plsprs(i,1)=0;

if nargin == 7, i = rank; end

for n = 1:i
	x = av';
	c = 0;
	for h=1:n
		t(:,h) = x * w(h,:)';
		x = x - t(:,h) * p(h,:);
		c = c + b(h) * t(:,h) * q(h,:);
	end
	c = c';
	error = c - cv;
   	plsprs(n, 1) = sum(sum(error .* error));
end
