function [final] = fuzzyswarm2(M,S,bodysize);
swarm = [];
for i = 1:size(S,2)                 % Loop over each group.
   GI = S(i).members;              % Find the indices into M for the group members' 
   %  rows.
   
   G = M(GI',:);                   % The group matrix, G, is built from these rows. 
   in = [];
   itsthere = 0;
   
   g_size = size(G,1);             % The number in the group, g_size, is simply the
   %  number of rows in G.
   
   % The original index only considers groups larger than 3; so, a check is now 
   % performed to see that the group meets this criterion, and completes the
   % calculations only if it does.  Otherwise, matrix SWARM is left empty.
   if g_size > 3                       % Checks for group of more than 3.
      
      K = convhull(G(:,2),G(:,3));    % The convhull function is used to find the
      %  perimeter individuals.
      perimeter_count = (size(K,1)-1);                                  
      
      for j = 1:(size(K,1)-1)         % This will loop over all perimeter segments.
         
         P = G(K(j,1),:);            % P is the first endpoint of the segment
         P2 = G(K(j+1,1),:);        % P2 is the other endpoint.
         for qq = 1:size(G,1)
            P
            P2
            point = [G(qq,1), G(qq,2), G(qq,3)]
            
            dist_point_to_line(P(1,2),P(1,3),P2(1,2),P2(1,3),G(qq,2),G(qq,3))<.5*bodysize
            if (dist_point_to_line(P(1,2),P(1,3),P2(1,2),P2(1,3),G(qq,2),G(qq,3))<.5*bodysize)
               for kk = 1:size(in,2)
                  if G(qq,1)==in(kk)
                     itsthere = 1;
                  end
               end
               if itsthere == 0
                  in = [in;G(qq)];
               end
               itsthere = 0;
            end
         end
         
         
         
         
      end
      
      % Now, the swarming index for this group is calculated, gswarm.  It is put 
      % in a matrix SWARM.
      gswarm = perimeter_count/size(G,1);
      
      swarm = [swarm; gswarm];     
   end                         % Ends the size check loop.
end                        % Ends the group loop.


% Since groups of 3 or less will have been ignored, a final check must be performed.
% If there were any 'legal' groups at all, SWARM will have been filled with their 
% values.  So, if SWARM is not empty at this point, the mean of it is taken (i.e. the
% average index for the array is found).  If there were no legal groups, i.e. only 
% stragglers/pairs/triplets, then -999 is entered for the index (which can easily be 
% weeded out later). 
final = swarm;
