function [GNUMS,mgnum] = meanGroupSize2d(M,S);
% MGROUPSZ(M,S) --  Finds the mean group size of array 'M' (in standard format), given 
% also 'S', which is a structure output of 'findgroups2d'.


GNUMS = [];                 % Initialize group counts matrix.

% Now, each group (found by findgroups2d) is picked out of the structure 'S', which
% holds the indices into 'M' of each group.  The size of each is found, and stored
% in the GNUMS matrix.
for i = 1:size(S,2)                    % Loop over each group.
    
    G = S(i).members;           % Pull out single group.
    
    n = size(G,2);              % Find the size of the group.
    
    GNUMS = [GNUMS; n];          % Builds the matrix.
end

% Finally, the mean size is taken (over all elements of GNUMS).
mgnum = mean(GNUMS);