
function C = count_loc(loc,ref,D)

% COUNT_LOC.M computes local K-Functions for k_count_loc.m

% Written by: TONY E. SMITH, 5/11/99

% INPUTS:
%     (i)   loc  = file of point locations (xi,yi), i=1:N
%		(ii)  ref  = file of reference locations (xk,yk), k =1:n
%     (iii) D    = vector of specified distance values
%
% OUTPUTS: DAT = (n:b) vector containing Point Counts

% List of Variables:
% N,n,D,k,ub,C,n,bb,bin,Tot

%Assertions

%mbrealvector(D);

% Initialize values

N = length(loc(:,1)) ; %number of initial points

n = length(ref(:,1)) ; %number of reference points

b = length(D); %number of distance values specified

C = zeros(n,b) ; %Matrix of counts

ub = ones(b,1);

Dmax = D(b) ; %Maximum distance value

% Start

for i = 1:n
   
   % Compute distances from i to loc points
   
   Freq = zeros(1,b) ; %Vector of frequency counts
   
   for j = 1:N
      
      Dist = sqrt((ref(i,1) - loc(j,1))^2 + ...
         (ref(i,2) - loc(j,2))^2); 
      
      %Add to point frequencies      
            
      if Dist <= Dmax  %ignores distances larger than max in D
         
         for k = 1:b           
            
                      
            if Dist <= D(k)
               
               Freq(k) = Freq(k) + 1 ; %adds to frequency count
               
               break
               
            end
            
         end         
         
      end  %end frequency count for ref point i    
               
   end
                  
   
   %Construct Cumulative Frequencies

	
   for k = 1:b-1
      
      Freq(k + 1) = Freq(k + 1) + Freq(k) ;
      
   end
   
   C(i,:) = Freq ;
   
end




