Documentation

Ordinal Categorical Arrays

Order of Categories

categoricalis 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 treatordinalcategorical arrays as having a mathematical ordering to their categories. Use an ordinal categorical array if you want to use the functionsmin,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.

ordinalcategorical arrays contain categories that have a meaningful mathematical ordering. For example, the discrete set of size categories{'small', 'medium', 'large'}has the mathematical orderingsmall < 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 using thecategoricalfunction with the'Ordinal',truename-value pair argument.

Ordinal Categorical Array from a Cell Array of Character Vectors

Create an ordinal categorical array,sizes, from a cell array of character vectors,A. Usevalueset, specified as a vector of unique values, to define the categories forsizes.

A = {'medium''large';'small''medium';'large''small'}; valueset = {'small','medium','large'}; sizes = categorical(A,valueset,'Ordinal',真正的)
sizes =3x2 categorical arraymedium large small medium large small

sizesis 3-by-2 ordinal categorical array with three categories such thatsmall < medium < large. The order of the values invaluesetbecomes the order of the categories ofsizes.

Ordinal Categorical Array from Integers

创建一个等价分类数组从加勒比海盗ay of integers. Use the values1,2, and3to define the categoriessmall,medium, andlarge, respectively.

A2 = [2 3; 1 2; 3 1]; valueset = 1:3; catnames = {'small','medium','large'}; sizes2 = categorical(A2,valueset,catnames,'Ordinal',真正的)
sizes2 =3x2 categorical arraymedium large small medium large small

Comparesizesandsizes2

isequal(sizes,sizes2)
ans =logical1

sizesandsizes2are equivalent categorical arrays with the same ordering of categories.

Convert a Categorical Array from Nonordinal to Ordinal

Create a nonordinal categorical array from the cell array of character vectors,A.

sizes3 = categorical(A)
sizes3 =3x2 categorical arraymedium large small medium large small

Determine if the categorical array is ordinal.

isordinal(sizes3)
ans =logical0

sizes3is a nonordinal categorical array with three categories,{'large','medium','small'}. The categories ofsizes3are the sorted unique values fromA. You must use the input argument,valueset, to specify a different category order.

Convertsizes3to an ordinal categorical array, such thatsmall < medium < large.

sizes3 = categorical(sizes3,{'small','medium','large'},'Ordinal',真正的);

sizes3is now a 3-by-2 ordinal categorical array equivalent tosizesandsizes2.

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 seeWork with Protected Categorical Arrays.

See Also

|||

Related Examples

More About