
function Ldat = L_func(a,loc)

% This program computes L-function.
% It requires AREA, a , to be given along with 
% a data file containing loacations: loc(i)=(Xi, Yi) 

[m,n] = size(loc); %loc = data matrix

i = 1;

D = zeros(m*(m-1)/2, 3) ;

% Start procedure.

i = 1 ;

k = 1 ;  %counter for D.

Dmax = 0 ;

while i < m
   
   j = i + 1 ;
   
   while j <= m
      
      D(k,1) = norm(loc(i,:) - loc(j,:));

      D(k,2) = i ;
      
      D(k,3) = j ;
      
           
      if D(k,1) > Dmax 
         
         Dmax = D(k,1);
         
      end
      
      j = j + 1 ;
      
      k = k + 1 ;
      
   end
   
   i = i + 1 ;
   
end

[M,n] = size(D) ;

D = sortrows(D) ;

%Count distinct distance values

i = 2 ;

ind = 1 ;

while i <= M 
   
   if D(i,1) > D(i-1,1)   
      
      ind = ind + 1 ;      
      
   end
   
   i = i + 1 ;
   
end

%Create final list

DD = zeros(ind,2) ;

DD(1,1) = 2 ;
DD(1,2) = D(1,1) ; 

i = 2;

ind = 1 ;

while i <= M
   
   if D(i,1) > D(i-1,1) 
      
      ind = ind + 1 ; 
      
      DD(ind,1) = DD(ind - 1,1) + 2 ; %includes ij and ji
      
      DD(ind,2) = D(i,1) ;
      
   else
      
      DD(ind,1) = DD(ind,1) + 1 ;
      
   end
   
   i = i + 1 ;
   
end

%Compute K

K = (a/(m^2)) * DD(:,1) ; 

%Compute LL

L = sqrt(K/pi) - DD(:,2) ;

Ldat = zeros(M,2) ;

Ldat(:,1) = L ;

Ldat(:,2) = DD(:,2) ;


