Documentation Center

  • Trial Software
  • Product Updates

ordinal class

Superclasses: categorical

Arrays for ordinal categorical data

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.


Ordinal arrays are used to store discrete values that have an ordering but are not numeric. An ordinal array provides efficient storage and convenient manipulation of such data, while also maintaining meaningful labels for the values. Ordinal arrays are often used as grouping variables.

Like a numerical array, an ordinal array can have any size or dimension. You can subscript, concatenate, reshape, sort, etc. ordinal arrays, much like ordinary numeric arrays. You can make comparisons between elements of two ordinal arrays, or between an ordinal array and a single string representing a ordinal value.


Use the ordinal constructor to create an ordinal array from a numeric, logical, or character array, or from a cell array of strings.

ordinalConstruct ordinal categorical array


Each ordinal array carries along a list of possible values that it can store, known as its levels. The list is created when you create an ordinal array, and you can access it using the getlevels method, or modify it using the addlevels, mergelevels, or droplevels methods. Assignment to the array will also add new levels automatically if the values assigned are not already levels of the array. The ordering on values stored in an ordinal array is defined by the order of the list of levels. You can change that order using the reorderlevels method.

The following table lists operations available for ordinal arrays.

sortSort elements of ordinal array
sortrowsSort rows
summarySummary statistics for ordinal array

Inherited Methods

Methods in the following table are inherited from categorical.

addlevelsAdd levels to categorical array
catConcatenate categorical arrays
cellstrConvert categorical array to cell array of strings
charConvert categorical array to character array
circshiftShift categorical array circularly
ctransposeTranspose categorical matrix
dispDisplay categorical array
displayDisplay categorical array
doubleConvert categorical array to double array
droplevelsDrop levels
endLast index in indexing expression for categorical array
flipdimFlip categorical array along specified dimension
fliplrFlip categorical matrix in left/right direction
flipudFlip categorical matrix in up/down direction
getlabelsAccess categorical array labels
getlevelsGet categorical array levels
histPlot histogram of categorical data
horzcatHorizontal concatenation for categorical arrays
int16Convert categorical array to signed 16-bit integer array
int32Convert categorical array to signed 32-bit integer array
int64Convert categorical array to signed 64-bit integer array
int8Convert categorical array to signed 8-bit integer array
intersectSet intersection for categorical arrays
ipermuteInverse permute dimensions of categorical array
isemptyTrue for empty categorical array
isequalTrue if categorical arrays are equal
islevelTest for levels
ismemberTrue for elements of categorical array in set
isscalarTrue if categorical array is scalar
isundefinedTest for undefined elements
isvectorTrue if categorical array is vector
lengthLength of categorical array
levelcountsElement counts by level
mergelevelsMerge levels
ndimsNumber of dimensions of categorical array
numelNumber of elements in categorical array
permutePermute dimensions of categorical array
reorderlevelsReorder levels
repmatReplicate and tile categorical array
reshapeResize categorical array
rot90Rotate categorical matrix 90 degrees
setdiffSet difference for categorical arrays
setlabelsLabel levels
setxorSet exclusive-or for categorical arrays
shiftdimShift dimensions of categorical array
singleConvert categorical array to single array
sizeSize of categorical array
squeezeSqueeze singleton dimensions from categorical array
subsasgnSubscripted assignment for categorical array
subsindexSubscript index for categorical array
subsrefSubscripted reference for categorical array
timesProduct of categorical arrays
transposeTranspose categorical matrix
uint16Convert categorical array to unsigned 16-bit integers
uint32Convert categorical array to unsigned 32-bit integers
uint64Convert categorical array to unsigned 64-bit integers
uint8Convert categorical array to unsigned 8-bit integers
unionSet union for categorical arrays
uniqueUnique values in categorical array
vertcatVertical concatenation for categorical arrays


Inherited Properties

Properties in the following table are inherited from categorical.

labelsText labels for levels
undeflabelText label for undefined levels

Copy Semantics

Value. To learn how this affects your use of the class, see Comparing Handle and Value Classes in the MATLAB Object-Oriented Programming documentation.


expand all

Create and Compare Ordinal Arrays

Create an ordinal array from integer data.

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

     low         medium      high 
     high        medium      low  
     medium      low         high 

Identify the elements in quality that are members of a level that is greater than or equal to 'medium'.

quality >= 'medium'
ans =

     0     1     1
     1     1     0
     1     0     1

A value of 1 in the resulting array ans indicates that the corresponding element of quality is a member of a level that is greater than or equal to 'medium'.

Identify the elements of quality that are members of either 'low' or 'high'.

ismember(quality,{'low' 'high'})
ans =

     1     0     1
     1     0     1
     0     1     1

A value of 1 in the resulting array ans indicates that the corresponding element of quality is a member of either 'low' or 'high'.

Create a second nominal array quality2 by flipping quality from left to right.

quality2 = fliplr(quality)
quality2 = 

     high      medium      low    
     low       medium      high   
     high      low         medium 

Compare the elements of quality and quality2.

quality == quality2
ans =

     0     1     0
     0     1     0
     0     1     0

A value of 1 in the resulting array ans indicates that the corresponding element of quality is a member of the same level as the corresponding element of quality2.


[1] Johnson, N. L., S. Kotz, and A. W. Kemp, Univariate Discrete Distributions, 2nd edition, Wiley, 1992, pp. 124-130.

See Also


Was this topic helpful?