This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


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 ordinal.

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 an ordinal array from integer data, providing explicit labels.

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

  1×9 ordinal array

  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 = 

  5×2 ordinal array

     large       small  
     large       small  
     small       medium 
     large       large  
     medium      large  

Input Arguments

collapse all

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

Labels for the discrete levels, specified as a character array or cell array of character vectors. 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

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.

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

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 character vector (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 character vector, or an undefined element.

Introduced in R2007a

Was this topic helpful?