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

    %% Group Data into Categorical Array
% Group numeric data into a categorical array. Use the result to confirm
% the amount of data that falls within 1 standard deviation of the mean
% value.
%
% Group normally distributed data into bins according to the distance from
% the mean, measured in standard deviations.
X = randn(1000,1);
edges = std(X)*(-3:3);
Y = discretize(X,edges, 'categorical', ...
    {'-3sigma', '-2sigma', '-sigma', 'sigma', '2sigma', '3sigma'});

%%
% |Y| contains undefined categorical values for the elements in |X| that
% are farther than 3 standard deviations from the mean.
%
% Preview the values in |Y|. 
Y(1:15)

%%
% Confirm that approximately 68% of the data falls within one standard
% deviation of the mean.
nnz(Y=='-sigma' | Y=='sigma')/numel(Y)