www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/UseGroupNumbersToSplitDataExample.m

    %% Use Group Numbers to Split Data
% Use group numbers to split patient height measurements into groups by gender. 
% Then calculate the mean height for each group.
%
% Load patient heights and genders from the data file |patients.mat|.

% Copyright 2015 The MathWorks, Inc.

load patients
whos Gender Height

%%
% Specify groups by gender with |findgroups|.
G = findgroups(Gender);

%% 
% Compare the first five elements of |Gender| and |G|. Where |Gender| contains 
% |'Female'|, |G| contains |1|. Where |Gender| contains |'Male'|, |G| contains |2|.
Gender(1:5)

%%
G(1:5)

%%
% Split the |Height| variable into two groups of heights using |G|. Apply the 
% |mean| function. The groups contain the mean heights of female and male patients, 
% respectively.
splitapply(@mean,Height,G)