function [ClusterA,ClusterB]=txclhi(m_x,r_xx,M_h,N_h,w,TestCrit,Verbose)
% TXCLHI  Make efficient groupings of textures

N_T=length(m_x);
[M_r,N_r]=size(r_xx); M_r=M_r/N_T;
Clusters=[];

if exist('Verbose')~=1, Verbose=2; end
if Verbose>=1
  fprintf(1,'Clustering textures\n');
end

% Find the two most distant textures according to the selected criterion
J_max=-Inf;
for Tex1=1:N_T-1
  for Tex2=i+1:N_T
    Tex=[Tex1 Tex2];
    h=moptxf(m_x,r_xx,M_h,N_h,TestCrit,w,Tex1,Tex2);
    for k=1:2
      m_v(k)=fstats(m_x(Tex(k)),r_xx((1:M_r)+(Tex(k)-1)*M_r,:),h,w);
    end
    J=meancrit(TestCrit,m_v(1),m_v(2));
    if J>J_max
      Clusters(1:2,1)=Tex';
      J_max=J;
    end
  end
end

% Add remaining textures to either group
for k=notin(1:N_T,Clusters(:))
  for c=1:2
    Clusters(c,k)=k;
    Clusters(3-c,k)=0;
    Clust1=nonzero(Clusters(1,:));
    Clust2=nonzero(Clusters(2,:));
    h=moptxf(m_x,r_xx,M_h,N_h,TestCrit,w,Clust1,Clust2);
    for l=1:N_T
      m_v(l)=fstats(m_x(l),r_xx((1:M_r)+(l-1)*M_r,:),h,w);
    end
    J(c)=meancrit(TestCrit,m_v(Clust1),m_v(Clust2));
  end
  if J(1)>J(2), Clusters(1,k)=k; Clusters(2,k)=0;
  else,         Clusters(1,k)=0; Clusters(2,k)=k;
  end
end

ClusterA=Clusters(1,:);
ClusterB=Clusters(2,:);

% Recursively divide each cluster into subclusters
for k=1:2
  m_x_sub=[];
  r_xx_sub=[];
  for l=nonzero(Clusters(k,:))
    m_x_sub =[m_x_sub ;m_x(l)];
    r_xx_sub=[r_xx_sub;r_xx((1:M_r)+(l-1)*M_r,:)];
  end
  [NewClustA,NewClustB]=txclhi(m_x_sub,r_xx_sub,M_h,N_h,w,TestCrit,Verbose);
  ClusterA(size(ClusterA,1)+
end