nominal

Create nominal array

After creating a nominal array, you can use related functions to add, drop, or merge categories, and more.

For more information, see Using nominal Objects.

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(X)
  • B = nominal(X,labels) example
  • B = nominal(X,labels,levels)
  • B = nominal(X,labels,[],edges)

Description

B = nominal(X) creates a nominal array B from the array X. nominal creates the levels of B from the sorted unique values in X, and creates default labels for them.

example

B = nominal(X,labels) labels the levels in B according to labels.

B = nominal(X,labels,levels) creates a nominal array with possible levels defined by levels.

B = nominal(X,labels,[],edges) creates a nominal array by binning a numeric array X with bin edges given by the numeric vector edges.

Examples

expand all

Create and Label Nominal Arrays

Create nominal arrays from a cell array of strings and from integer data, and provide explicit labels.

Create a nominal array from a cell array of strings with values 'r', 'g', and 'b'. Label these levels 'red', 'green', and 'blue', respectively. Note that the labels are specified according to the sorted (alphabetical) order of the elements in X.

X = {'r' 'b' 'g';'g' 'r' 'b';'b' 'r' 'g'}
B = nominal(X,{'blue','green','red'})
X = 

    'r'    'b'    'g'
    'g'    'r'    'b'
    'b'    'r'    'g'


B = 

     red        blue      green 
     green      red       blue  
     blue       red       green 

Create a nominal array from integer data with values 1 to 4, merging odd and even values into two nominal levels with labels 'odd' and 'even'. Achieve the merging by duplicating the labels.

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

     4     1
     4     2
     1     3
     4     4
     3     4


B = 

     even      odd  
     even      even 
     odd       odd  
     even      even 
     odd       even 

Input Arguments

expand all

X — Input arraynumeric | logical | character | categorical | cell array of strings

Input array to convert to nominal, specified as a numeric, logical, character, or categorical array, or a cell array of strings. The levels of the resulting nominal array correspond to the sorted unique values in X.

labels — Labels for the discrete levelscharacter array | cell array of strings

Labels for the discrete levels, specified as a character array or cell array of strings. By default, nominal assigns the labels to the levels in B in order according to the sorted unique values in X.

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

Data Types: char | cell

levels — Possible nominal levelsvector

Possible nominal levels for the output nominal array, specified as a vector whose values can be compared to those in X using the equality operator. nominal assigns labels to each level from the corresponding elements of labels. If X contains any values not present in levels, the levels of the corresponding elements of B are undefined.

edges — Bin edgesnumeric vector

Bin edges to create a nominal array by binning a numeric array, specified as a numeric vector. The uppermost bin includes values equal to the right-most edge. nominal assigns labels to each level in the resulting nominal array from the corresponding elements of labels. When you specify edges, it must have one more element than labels.

Output Arguments

expand all

B — Nominal arraynominal array object

Nominal array, returned as a nominal array object.

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

See Also

Was this topic helpful?