Code covered by the BSD License  

Highlights from
Estimating the number of clusters via System Evolution

from Estimating the number of clusters via System Evolution by Kaijun Wang
estimate number of clusters for far clusters, small-larger clusters, slightly overlapping clusters

[A2,B2]=findnear(Dist,A1,na,B1,nb,n1)
function [A2,B2]=findnear(Dist,A1,na,B1,nb,n1)
% finding nearest elements between two groups A1 and B1
% A2 in A1 contains nearest elements near B1
% Dist - distance matrix, 0 is the smallest value

A2=[];
B2=[];
for i=1:100
  [c,ra]=min(Dist(A1,B1));
  [c,ia]=sort(c);
  for j=1:length(B1)
   c=ra(ia);
   c=unique(c);
   L=length(c);
   if L==na 
    break; 
   elseif L>na
    ia=ia(1:length(ia)-L+na);
   elseif L<na 
    if j==1 
        break; 
    end
    ia=ia(1:length(ia)+1);
   end
  end
  if length(A1)==1 
      c=1; 
  end
  A2=[A2;A1(c)];
  if L>=na 
      break; 
  end
  A1=setdiff(A1,A1(c));
  na=na-L;
end

if n1==1 
    return; 
end

for i=1:100
  [c,rb]=min(Dist(B1,A1));
  [c,ib]=sort(c);
  for j=1:length(A1)
   c=rb(ib);
   c=unique(c);
   L=length(c);
  if L==nb 
    break; 
  elseif L>nb
    ib=ib(1:length(ib)-L+nb);
   elseif L<nb 
    if j==1 
        break; 
    end
    ib=ib(1:length(ib)+1);
  end
  end
  if length(B1)==1 
      c=1; 
  end
  B2=[B2;B1(c)];
  if L>=nb 
      break; 
  end
  B1=setdiff(B1,B1(c));
  nb=nb-L;
end

Contact us at files@mathworks.com