MATLAB Examples

Create Nominal Arrays

This example shows how to create nominal arrays using nominal.

Contents

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 array

    {'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 array

     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 array

    {'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 array

    {'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.