function [c, x] = plspred(a, p, q, w, b, n)
% PLSPRED   Predicts concentrations in unknown spectra.
% Copyright (c) 1989-92 by The MathWorks, Inc.
%
%             PLSPRED predicts concentrations in unknown spectra using
%             desired number of factors from the calibration 
%             generated by PLSF.M
%
%  c = plspred(a, p, q, w, b, n)  or  [c, x] = plspred(a, p, q, w, b, n)
%
% Where:
%
%  c    is the matrix of the unknown's concentrations
%  x    is the residual after decomposition of the unknown spectrum or spectra
%  a    is the unknown spectrum (or spectra)
%  p    is the matrix of spectral factors
%  q    is the matrix of concentration factors
%  w    is the matrix of weights
%  b    is the vector containing the inner relationships
%  n    is the number of factors to use
%
x = a';
c = 0;
[maxrank, h] = size(p);
if n > maxrank, n = maxrank, end;
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';
