function c=discanal(x,A,k,h1,h2)

% DISCANAL    Non-parametric discriminant analysis.
%
%             DISCANAL(X,A,K,H1,H2) uses a multivariate Kernel Density 
%             Estimate (with kernel given by the function in K) to distinguish
%             in which of two data sets the points X 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.

g=A(:,e); % Extract the final column of A which determines the grouping of
          % the data.

% Split the data into two groups based on the g vector.

c1=1;
c2=1;

for i=1:n,
  if g(i)==1,
    g1(c1,1:d)=A(i,1:d);
    c1=c1+1;
    else,
      g2(c2,1:d)=A(i,1:d);
      c2=c2+1;
  end;
end;

[n1 d]=size(g1); % n1 is the number of entries in data set 1.

[n2 d]=size(g2); % ditto for n2.

if nargin==3,  % If no value of h has been specified by the user then 
               % calculate one.
  if k=='kmvn',  % Normal kernel
    a=(4/(2*d+1))^(1/(d+4));
  end; 

  h1=a*n1^(-1/(d+4)); % Calculate 'optimum' value of h1.

  h2=a*n2^(-1/(d+4)); % Calculate 'optimum' value of h2.

end;

z1=mvkdest(x,g1,k,h1);

z2=mvkdest(x,g2,k,h2);

c=z2>z1; 

c=c+1;