%% Return Nonscalar Output for Groups
% Calculate the minimum, median, and maximum weights for groups of patients and return these results as arrays for each group. splitapply concatenates the output arguments so that you can distinguish output for each group from output for the other groups.
% Define a function that returns the minimum, median, and maximum as a row vector.

mystats = @(x)[min(x) median(x) max(x)];

%
% Load patient weights, genders, and status as smokers from patients.mat.

load patients
whos Weight Gender Smoker

%
% Use findgroups and splitapply to split the patient weights into groups and calculate statistics for each group.

G = findgroups(Gender,Smoker);
Y = splitapply(mystats,Weight,G)

% In this example, you can return nonscalar output as row vectors because the data and grouping variables are column vectors. Each row of Y contains statistics for a different group of patients.
% Copyright 2012 The MathWorks, Inc.