function [categories reduced_values] = group( group_data, group_by, ftn )
%GROUP Group values using a given function
%   For each unique value in group_by, use ftn to reduce the corresponding
%   values in group_data to a single value.
% Returns a list of unique values from the group_by argument and 
% a corresponding list of reduced values.
    categories = unique(group_by);
    reduced_values = zeros(length(categories),1);
    n = 1;
    for i = 1:length(categories)
        category = categories(i);
        values = group_data(category==group_by);
        values = values(~isnan(values));
        reduced_values(n) = ftn(values);       
        n = n + 1;
    end;
end