
function DAT = k2ct_diff(m,Dist,D)

% This program computes k-counts for k2_diff

% Written by: TONY E. SMITH, 1/28/02

% INPUTS:
%     (i)      m = number of point
%     (ii)  Dist = output of dist_list (dij,i,j), i=1..m
%     (iii)   D  = vector of distance values (ordered low to high)

% OUTPUTS: DAT = (D x 1) vector of counts

%compiling initialization

%mbrealvector(D);

%Count point frequencies

B = length(D);

C = zeros(m,B) ;

[M,junk] = size(Dist);

if M ~= m*(m-1)/2
   
   error('Dist has wrong dimension');
   
end


Dmax = max(D); %maximum relevant distance

%mbrealscalar(Dmax);

Dmin = min(D); %minimum relevant distance

%mbrealscalar(Dmin);


k = 1 ;

while k <= M 
   
  
   if Dist(k,1) <= Dmax
                        
      i = Dist(k,2) ;
      
      j = Dist(k,3) ; 
      
      if Dist(k,1) <= Dmin
         
         C(i,:) = C(i,:) + 1;
         
         C(j,:) = C(j,:) + 1;
         
      else
         
         dist = max([Dist(k,1),Dmin]);
         
         %mbrealscalar(dist);
         
         b = B ;
         
         while D(b) >= dist
         
         	C(i,b) = C(i,b) + 1 ;
         
         	C(j,b) = C(j,b) + 1 ; %add to both i and j 
         
         	b = b - 1 ;
         
         end
         
      end %end if >=
      
   end %end if <=
   
   k = k + 1 ;
   
end

%Compute Output

   
DAT = sum(C) ; %sum columns of C to get total counts
   


