www.gusucode.com > stats 源码程序 matlab案例代码 > stats/MergeCategoryLevelsExample.m
%% Merge Category Levels % This example shows how to merge categories in a categorical array using % |mergelevels|. This is useful for collapsing categories with few observations. % Copyright 2015 The MathWorks, Inc. %% Load sample data. load carsmall %% Create a nominal array. % The variable |Origin| is a character array containing the country of origin % for 100 sample cars. Convert |Origin| to a nominal array. Origin = nominal(Origin); getlevels(Origin) %% % There are six unique countries of origin in the data. %% Tabulate category counts. % Explore the elements of the categorical array. tabulate(Origin) %% % There are relatively few observations in each European country. %% Merge categories. % Merge the categories |France|, |Germany|, |Italy|, and |Sweden| into one % category called |Europe|. Origin = mergelevels(Origin,{'France','Germany','Italy','Sweden'},... 'Europe'); getlevels(Origin) %% % The variable |Origin| now has only three category levels. %% Tabulate category counts. % Explore the elements of the merged categories. tabulate(Origin) %% % The category |Europe| has the 16% of observations that were previously % distributed across four countries.