www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/UseCustomFunctionsExample.m
%% Use Custom Functions %% % Create a vector of data, |val|, and a matrix of subscripts, |subs|. val = [100.1 101.2 103.4 102.8 100.9 101.5]'; subs = [1 1; 1 1; 2 2; 3 2; 2 2; 3 2] %% % The subscripts in |subs| define a 3-by-2 matrix for the output. %% % Use the |fun| input of |accumarray| to calculate the within-group variances % of data in |val| that have identical subscripts in |subs|. Specify |fun| % as |@var|. A1 = accumarray(subs,val,[],@var) %% % The result is a 3-by-2 matrix of variance values. %% % Alternatively, you can specify |fun| as an anonymous function so long % as it accepts vector inputs and returns a scalar. A common situation where % this is useful is when you want to pass additional parameters to a function. % In this case, use the |var| function with a normalization parameter. A2 = accumarray(subs,val,[],@(x) var(x,1)) %% % The result is a 3-by-2 matrix of normalized variance values.