function ALL = allnnfuzzy(ARRAY, bodysize);

% allnnfuzzy(<array>, bodysize) -- Find nearest neighbors for all points in an array.
%
% Finds all nearest neighbors for every individual.  If any individuals are within
% .5*bodysize of the nearest neighbor distance from the focus individual, they are 
% included as neighbors.
%

% Finds nearest neighbors, and those individuals within .5*bodysize of the nn's.
% Created by Jim Work, University of South Carolina, August 13, 2003



DIST = [];  						% initialize the DISTance matrix

N = size(ARRAY,1);			  

for n = 1:N							% loop over every point and find distance
   for m = 1:N						% for each other point in the group
      if (m == n)
         DIST(n,m) = inf;
      else
         x1 = ARRAY(n,2);
         y1 = ARRAY(n,3);
         z1 = ARRAY(n,4);
         x2 = ARRAY(m,2);
         y2 = ARRAY(m,3);
         z2 = ARRAY(m,4);
         DIST(n,m) = dist3d(x1,y1,z1,x2,y2,z2);
      end		
   end
end

currentlow = inf;
q = 0;
final = [];

for i = 1:size(DIST,1)							% Find the nearest neighbor's distance from
   for j = 1:size(DIST,2)						% the focus individual
      
      if (DIST(i,j) < currentlow)
         currentlow = DIST(i,j);
      end
   end
   available = 1;											% Add all neighbors that are less than
   for j = 1:size(DIST,2)								% nnd + .5*(bodysize) to the final array
     if (DIST(i,j)<(currentlow+(.5*bodysize)))
        final (i,available) = j;	
        available = available + 1;         
      end
   end
   
   currentlow = inf;
end

final = addIndex(final);

ALL = final;
