Note
The nominal
and ordinal
array data types are not recommended. To represent ordered and unordered discrete, nonnumeric
data, use the Categorical Arrays data type instead.
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)
ans = 3x1 cell
{'setosa' }
{'versicolor'}
{'virginica' }
Create a nominal array.
Convert species
to a nominal array using the categories occurring in the data.
speciesNom = nominal(species); class(speciesNom)
ans = 'nominal'
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)
ans = 1x3 nominal
setosa versicolor virginica
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)
ans = 1x3 cell
{'setosa'} {'versicolor'} {'virginica'}
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)
ans = 1x3 cell
{'seto'} {'vers'} {'virg'}
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')
ans = logical
1
The logical value 1
indicates that the two labels, 'setosa'
and 'seto'
, correspond to the same observations.
This example shows how to create ordinal arrays using ordinal
.
Load sample data.
AllSizes = {'medium','large','small','small','medium',... 'large','medium','small'};
The created variable, AllSizes
, is a cell array of character vectors containing size measurements on eight objects.
Create an ordinal array.
Create an ordinal array with category levels and labels corresponding to the values in the cell array (the default levels and labels).
sizeOrd = ordinal(AllSizes); getlevels(sizeOrd)
ans = 1x3 ordinal
large medium small
Explore category labels.
By default, ordinal
uses the original character vectors as category labels. The default order of the categories is ascending alphabetical order.
getlabels(sizeOrd)
ans = 1x3 cell
{'large'} {'medium'} {'small'}
Add additional categories.
Suppose that you want to include additional levels for the ordinal array, xsmall
and xlarge
, even though they do not occur in the original data. To specify additional levels, use the third input argument to ordinal
.
sizeOrd2 = ordinal(AllSizes,{},... {'xsmall','small','medium','large','xlarge'}); getlevels(sizeOrd2)
ans = 1x5 ordinal
xsmall small medium large xlarge
Explore category labels.
To see which levels are actually present in the data, use unique
.
unique(sizeOrd2)
ans = 1x3 ordinal
small medium large
Specify the category order.
Convert AllSizes
to an ordinal array with categories small
< medium
< large
. Generally, an ordinal array is distinct from a nominal array because there is a natural ordering for levels of an ordinal array. You can use the third input argument to ordinal
to specify the ascending order of the levels. Here, the order of the levels is smallest to largest.
sizeOrd = ordinal(AllSizes,{},{'small','medium','large'}); getlevels(sizeOrd)
ans = 1x3 ordinal
small medium large
The second input argument for ordinal
is a list of labels for the category levels. When you use braces {}
for the level labels, ordinal
uses the labels specified in the third input argument (the labels come from the levels present in the data if only one input argument is used).
Compare elements.
Verify that the first object (with size medium
) is smaller than the second object (with size large
).
sizeOrd(1) < sizeOrd(2)
ans = logical
1
The logical value 1
indicates that the inequality holds.
getlabels
| getlevels
| nominal
| ordinal