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

    %% Split Table Data Variables and Apply Function
% Calculate the mean body-mass-index (BMI) from tables of patient data. Group 
% the patients by gender and status as smokers or nonsmokers.
%
% Load patient data and grouping variables into tables.

% Copyright 2015 The MathWorks, Inc.

load patients
DT = table(Height,Weight);
GT = table(Gender,Smoker);

%% 
% Define a function that calculates mean BMI from the weights and heights of 
% groups or patients.
meanBMIFcn = @(h,w)mean((w ./ (h.^2)) * 703);

%%
% Create a table that contains the mean BMI for each group.
[G,results] = findgroups(GT);
meanBMI = splitapply(meanBMIFcn,DT,G);
results.meanBMI = meanBMI