Create ordinal array

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

For more information, see Using ordinal 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.


  • B = ordinal(X)
  • B = ordinal(X,labels)
  • B = ordinal(X,labels,levels)
  • B = ordinal(X,labels,[],edges)


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


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

B = ordinal(X,labels,levels) creates an ordinal array with possible levels defined by levels.


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


collapse all

Create and Label Ordinal Arrays

Create an ordinal array from integer data, providing explicit labels.

quality = ordinal([1 2 3 3 2 1 2 1 3],...
   {'low' 'medium' 'high'})
quality = 

  Columns 1 through 7

     low      medium      high      high      medium      low      medium 

  Columns 8 through 9

     low      high 

Show that the first element is less than the second element (low is less than medium).

quality(1) < quality(2)
ans =


Create an ordinal array by binning values between 0 and 1 into thirds with labels 'small', 'medium', and 'large'.

X = rand(5,2)
A = ordinal(X,{'small' 'medium' 'large'},[],[0 1/3 2/3 1])
X =

    0.8147    0.0975
    0.9058    0.2785
    0.1270    0.5469
    0.9134    0.9575
    0.6324    0.9649

A = 

     large       small  
     large       small  
     small       medium 
     large       large  
     medium      large  

Related Examples

Input Arguments

collapse all

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

Input array to convert to ordinal, specified as a numeric, logical, character, or categorical array, or a cell array of strings. The levels of the resulting ordinal 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, ordinal 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 ordinal levelsvector

Possible ordinal levels for the output ordinal array, specified as a vector whose values can be compared to those in X using the equality operator. ordinal 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 ordinal array by binning a numeric array, specified as a numeric vector. The uppermost bin includes values equal to the right-most edge. ordinal 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

collapse all

B — Ordinal arrayordinal array object

Nominal array, returned as an ordinal 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

Introduced in R2007a

Was this topic helpful?