Documentation Center

  • Trial Software
  • Product Updates

nominal

Class: nominal

Construct nominal categorical array

The nominal and ordinal array data types might be removed in a future release. To represent ordered and unordered discrete, nonnumeric data, use the MATLAB® categorical data type instead.

Syntax

B = nominal(A)
B = nominal(A,labels)
B = nominal(A,labels,levels)
B = nominal(A,labels,[],edges)

Description

B = nominal(A) creates a nominal array B from the array A. A can be numeric, logical, character, categorical, or a cell array of strings. nominal creates the levels of B from the sorted unique values in A, and creates default labels for them.

B = nominal(A,labels) labels the levels in B using the character array or cell array of strings labels. nominal assigns labels to levels in B in order according to the sorted unique values in A.

B = nominal(A,labels,levels) creates a nominal array with possible levels defined by levels. levels is a vector whose values can be compared to those in A using the equality operator. nominal assigns labels to each level from the corresponding elements of labels. If A contains any values not present in levels, the levels of the corresponding elements of B are undefined.

B = nominal(A,labels,[],edges) creates a nominal array by binning the numeric array A with bin edges given by the numeric vector edges. The uppermost bin includes values equal to the right-most edge. nominal assigns labels to each level in B from the corresponding elements of labels. edges must have one more element than labels.

By default, an element of B is undefined if the corresponding element of A is NaN (when A is numeric), an empty string (when A is character), or undefined (when A is categorical). nominal treats such elements as "undefined" or "missing" and does not include entries for them among the possible levels for B. To create an explicit level for such elements instead of treating them as undefined, you must use the levels input, and include NaN, the empty string, or an undefined element.

You may include duplicate labels in labels in order to merge multiple values in A into a single level in B.

Examples

expand all

Create and Label Nominal Arrays

Load the sample data.

load fisheriris;

Create a nominal array and display a summary of the data.

species = nominal(species);
summary(species)
     setosa          50 
     versicolor      50 
     virginica       50 

Create a nominal array from characters, and provide explicit labels.

colors1 = nominal({'r' 'b' 'g'; 'g' 'r' 'b'; 'b' 'r' 'g'},...
   {'blue' 'green' 'red'})  
colors1 = 

     red        blue      green 
     green      red       blue  
     blue       red       green 

Create a nominal array from characters, and provide both explicit labels and an explicit order for display.

colors2 = nominal({'r' 'b' 'g'; 'g' 'r' 'b'; 'b' 'r' 'g'}, ...
           {'red' 'green' 'blue'},{'r' 'g' 'b'})
colors2 = 

     red        blue      green 
     green      red       blue  
     blue       red       green 

Create a nominal array from integer data, merging odd and even values into only two nominal levels. Provide explicit labels.

toss = nominal(randi([1 4],5,2),{'odd' 'even' 'odd' 'even'},1:4)
toss = 

     even      odd  
     even      even 
     odd       odd  
     even      even 
     odd       even 

Change Nominal Array Levels

Load the sample data.

load hospital;

Make the {0,1}-valued variable Smoker nominal, and change the labels to 'No' and 'Yes'.

hospital.Smoker = nominal(hospital.Smoker,{'No','Yes'});

Add new levels to Smoker as placeholders for more detailed histories of smokers.

hospital.Smoker = addlevels(hospital.Smoker,...
                 {'0-5 Years','5-10 Years','LongTerm'});

Assuming the nonsmokers have never smoked, relabel the 'No' level.

hospital.Smoker = setlabels(hospital.Smoker,'Never','No');

Drop the undifferentiated 'Yes' level from Smoker.

hospital.Smoker = droplevels(hospital.Smoker,'Yes');

Note that smokers now have an undefined level.

Set each smoker to one of the new levels, by observation name.

hospital.Smoker('YPL-320') = '5-10 Years';

See Also

|

Was this topic helpful?