Ordinal Categorical Arrays - MATLAB & Simulink (original) (raw)
Main Content
Order of Categories
categorical
is a data type to store data with values from a finite set of discrete categories, which can have a natural order. You can specify and rearrange the order of categories in all categorical arrays. However, you only can treat ordinal categorical arrays as having a mathematical ordering to their categories. Use an ordinal categorical array if you want to use the functions min
, max
, or relational operations, such as greater than and less than.
The discrete set of pet categories ["dog" "cat" "bird"]
has no meaningful mathematical ordering. You are free to use any category order and the meaning of the associated data does not change. For example, pets = categorical(["bird" "cat" "dog" "dog" "cat"])
creates a categorical array and the categories are listed in alphabetical order, ["bird" "cat" "dog"]
. You can choose to specify or change the order of the categories to ["dog" "cat" "bird"]
and the meaning of the data does not change.
Ordinal categorical arrays contain categories that have a meaningful mathematical ordering. For example, the discrete set of size categories ["small" "medium" "large"]
has the mathematical ordering small < medium < large
. The first category listed is the smallest and the last category is the largest. The order of the categories in an ordinal categorical array affects the result from relational comparisons of ordinal categorical arrays.
How to Create Ordinal Categorical Arrays
This example shows how to create an ordinal categorical array by using the categorical
function with the Ordinal
name-value argument.
Ordinal Categorical Array from String Array
Create an ordinal categorical array. To make the array ordinal, set the Ordinal
name-value argument to true
.
A = ["medium" "large";"small" "medium"; "large" "small"]; valueset = ["small" "medium" "large"];
sizes = categorical(A,valueset,Ordinal=true)
sizes = 3x2 categorical
medium large
small medium
large small
Ordinal Categorical Array from Integers
Create an equivalent categorical array from an array of integers. Use the values 1
, 2
, and 3
to define the categories small
, medium
, and large
, respectively.
A2 = [2 3; 1 2; 3 1]; valueset = 1:3; catnames = ["small" "medium" "large"];
sizes2 = categorical(A2,valueset,catnames,Ordinal=true)
sizes2 = 3x2 categorical
medium large
small medium
large small
Compare sizes
and sizes2
.
sizes
and sizes2
are equivalent categorical arrays with the same ordering of categories.
Convert a Categorical Array from Nonordinal to Ordinal
To convert an ordinal categorical array to a nonordinal array, use the categorical
function without the Ordinal
name-value argument.
sizes3 = categorical(sizes)
sizes3 = 3x2 categorical
medium large
small medium
large small
Determine if the categorical array is ordinal.
Convert sizes3
to an ordinal categorical array.
sizes3 = categorical(sizes3,Ordinal=true); isordinal(sizes3)
sizes3
is now a 3-by-2 ordinal categorical array equivalent to sizes
and sizes2
.
Working with Ordinal Categorical Arrays
In order to combine or compare two categorical arrays, the sets of categories for both input arrays must be identical, including their order. Furthermore, ordinal categorical arrays are always protected. Therefore, when you assign values to an ordinal categorical array, the values must belong to one of the existing categories. For more information see Work with Protected Categorical Arrays.
See Also
categorical | categories | isordinal | isequal
Related Examples
- Create Categorical Arrays
- Convert Text in Table Variables to Categorical
- Compare Categorical Array Elements
- Access Data Using Categorical Arrays