function [IIDS, GRP_IIDs] = interIndivDist2d(M,S);
% function [IIDS, GRP_IIDs] = interIndivDist2d(M,S);
% !!!! IIDS is the mean interindividual distance
% !!!! GRP_IIDs is the input to shape2d
% !!!! A returned value of -999.0000 indicates that there is only one indiv in the group
% ################## UPDATED 22 JUNE 2005: Jim Work #############################
% IID(M,S) --- Computes the average inter-individual distance for a given array of 
% points, 'M'.  For each point, the distance to every other point is found, and the
% average taken to find the mean IID for that point.  Then the mean over all points 
% is found and reported.  The second output is a cell containing max IID's for each
% individual, which are to be passed to the shape function later, and are not germane
% to the IID index.  The second input, S, is the structure output from the
% findgroups2d function.
  


% The cell for the max IID's is initialized.
GRP_IIDs = cell(size(S,2),1);


% IID will have to be calculated for each group, then a matrix built of 
% these values at the end.  So first, the groups must be isolated.
MGDIST = [];
for gn = 1:size(S,2)                % Big loop; over each group.
    
    
    % Now, pull out the numbers of the individuals in group gn.
    GIND = S(gn).members;
    % These individual points are pulled from the parent array M.
    GRP = [];
    for p = 1:size(GIND,2)          % Loops over all members.
        gind = GIND(1,p);           % Gets individual index.
        IND = M(gind,:);            % Pulls that row from array.
        GRP = [GRP; IND];           % And builds the group matrix, a subset of 
                                        %  the original M.
    end
    
    % Now we have our group isolated, and the IID can be calculated.
    DISTG = [];
    MAXDS = [];
    for i = 1:size(GRP,1)               % For each individual...
        
        DISTi = [];
        for j = 1:size(GRP,1)           % ...to each other...
            
            % ...calc. the distance.
            dist = dist2d(GRP(i,2),GRP(i,3),GRP(j,2),GRP(j,3));
            
            DISTi = [DISTi dist];       % 
        end
        
        % Calc. mean IID for individual i, and store it in a matrix 
        % containing the mean IID for each indiv.  If the group is a straggler, 
        % there is no IID, so a very low number is entered (for ease of stat 
        % analysis later).  The largest IID value for each point is also recorded, 
        % for use in calculating the shape index later.  
        if size(GRP,1) ~= 1                 % Check group size
            mdisti = sum(DISTi) / (size(GRP,1) - 1);
            maxdisti = max(DISTi);
        else                        % For size = 1, -999 is entered.
            mdisti = -999;
            maxdisti = -999;
        end
        
        % The matrices are built of the mean IID per ind., and the max IID for each.
        DISTG = [DISTG mdisti];
        MAXDS = [MAXDS maxdisti];
    end
    
    % Each matrix of group IIDs will be saved in a cell, so the max val. 
    % of each may be found later to calculate shape.
    GRP_IIDs{gn,1} = MAXDS;
    
    % The mean IID for the group is now found, by averaging the means of the 
    % individual IID's.  
    mdistg = mean(DISTG);
    MGDIST = [MGDIST; mdistg];
    
end

% The final IID matrix is a column containing the mean IID's for each group in the 
% field.  
IIDS = MGDIST;