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

    %% Apply Function to Groups Within Variables  
% Compute the group-wise means of variables in a table, |A|, and return
% them as rows in a table, |B|.   
%
% Create a table where one variable defines groups. 
A = table({'test2';'test1';'test2';'test3';'test1'},...
    [0.71;-2.05;-0.35;-0.82;1.57],[0.23;0.12;-0.18;0.23;0.41])  

%% 
% Define the anonymous function to find the mean of an input. 
func = @mean; 

%%
% |func| uses an existing MATLAB(R) function to define the operation.  
%
% Apply the function to each group of data defined by |Var1|. 
B = varfun(func,A,'GroupingVariables','Var1') 

%%
% |B| contains a variable called |GroupCount| to indicate the number of 
% entries from table |A| in that group.