function c=crossval(A,k,h1,h2)

% CROSSVAL    Cross validation for Non-parametric discriminant analysis.
%
%             CROSSVAL(A,K,H1,H2) uses cross validation together with a 
%             multivariate Kernel Density Estimate (with kernel given by the 
%             function in K) to distinguish in which of two data sets the 
%             points in A lie.
%
%             The multivariate data points (training sets) are stored row 
%             by row in A.  The final column of A should consist of 1's 
%             and 2's to indicate which of the two groups the data belongs.
%
%             The optional parameters H1 and H2 represent the window width
%             for each density estimate.  If omitted simplistic 'optimal' 
%             values are used.
%
%             Christian C. Beardah 1993

[n e]=size(A); % Establish dimensions of A, 
               % n=number of data points,
               % d=dimension of the data. 

d=e-1; % Dimensionality of the data.

for i=1:n,
  if nargin==2,
    g(i)=discanal(A(i,1:d),A([1:i-1 i+1:n],:),k);
		else,
		  g(i)=discanal(A(i,1:d),A([1:i-1 i+1:n],:),k,h1,h2);
	end;		
end;

t=(g'==A(:,e)); % Calculate 'truth' vector.

fprintf('There are %g mis-classifications out of %g data points \n',n-sum(t),n); 
