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

    %% Summary Statistics Grouped by Category  
% This example shows how to compute summary statistics grouped by levels
% of a categorical variable. You can compute group summary statistics for
% a numeric array or a dataset array using |grpstats|.   

% Copyright 2015 The MathWorks, Inc.


%% Load sample data. 
load hospital

%%
% The dataset array, |hospital|, has 7 variables (columns) and 100 observations
% (rows).  

%% Compute summary statistics by category. 
% The variable |Sex| is a nominal array with two levels, |Male| and |Female|.
% Compute the minimum and maximum weights for each gender. 
stats = grpstats(hospital,'Sex',{'min','max'},'DataVars','Weight') 

%%
% The dataset array, |stats|, has observations corresponding to the levels
% of the variable |Sex|. The variable |min_Weight| contains the minimum
% weight for each group, and the variable |max_Weight| contains the maximum
% weight for each group.  

%% Compute summary statistics by multiple categories. 
% The variable |Smoker| is a logical array with value |1| for smokers and
% value |0| for nonsmokers. Compute the minimum and maximum weights for
% each gender and smoking combination. 
stats = grpstats(hospital,{'Sex','Smoker'},{'min','max'},...
                 'DataVars','Weight') 

%%
% The dataset array, |stats|, has an observation row for each combination
% of levels of |Sex| and |Smoker| in the original data.