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

    %% Create Nominal Arrays  
% This example shows how to create nominal arrays using |nominal|.   

%% Load sample data. 
% The variable |species| is a 150-by-1 cell array of character vectors containing
% the species name for each observation. The unique species types are setosa,
% versicolor, and virginica. 
load fisheriris
unique(species)

%% Create a nominal array. 
% Convert |species| to a nominal array using the categories occurring in
% the data. 
speciesNom = nominal(species);
class(speciesNom)  

%% Explore category levels. 
% The nominal array, |speciesNom|, has three levels corresponding to the
% three unique species. The levels of a nominal array are the set of possible
% values that its elements can take. 
getlevels(speciesNom) 

%%
% A nominal array can have more levels than actually appear in the data.
% For example, a nominal array named |AllSizes| might have levels |small|,
% |medium|, and |large|, but you might only have observations that are |medium|
% and |large| in your data. To see which levels of a nominal array are actually
% present in the data, use |unique|, for instance, |unique(AllSizes)|.  

%% Explore category labels. 
% Each level has a label. By default,
% |nominal| labels the category levels with the values occurring in the
% data. For |speciesNom|, these labels are the species types. 
getlabels(speciesNom)  

%% Specify your own category labels. 
% You can specify your own labels for each category level. You can specify
% labels when you create the nominal array. 
speciesNom2 = nominal(species,{'seto','vers','virg'});
getlabels(speciesNom2) 

%%
% You can also change category labels on an existing nominal array using
% |setlabels|  

%% Verify new category labels. 
% Verify that the new labels correspond to the original labels in |speciesNom|. 
isequal(speciesNom=='setosa',speciesNom2=='seto') 

%%
% The logical value |1| indicates that the two labels, |'setosa'| and |'seto'|,
% correspond to the same observations.