Advantages of Using Categorical Arrays
In this section...
“Natural Representation of Categorical Data” on page 8-43
“Mathematical Ordering for Character Vectors” on page 8-43
“Reduce Memory Requirements” on page 8-43
Natural Representation of Categorical Data
categorical is a data type to store data with values from a finite set of discrete
categories. One common alternative to using categorical arrays is to use character arrays
or cell arrays of character vectors. To compare values in character arrays and cell arrays
of character vectors, you must use strcmp which can be cumbersome. With categorical
arrays, you can use the logical operator eq (==) to compare elements in the same way
that you compare numeric arrays. The other common alternative to using categorical
arrays is to store categorical data using integers in numeric arrays. Using numeric arrays
loses all the useful descriptive information from the category names, and also tends to
suggest that the integer values have their usual numeric meaning, which, for categorical
data, they do not.
Mathematical Ordering for Character Vectors
Categorical arrays are convenient and memory efficient containers for nonnumeric data
with values from a finite set of discrete categories. They are especially useful when the
categories have a meaningful mathematical ordering, such as an array with entries from
the discrete set of categories {'small','medium','large'} where small < medium
< large.
An ordering other than alphabetical order is not possible with character arrays or cell
arrays of character vectors. Thus, inequality comparisons, such as greater and less than,
are not possible. With categorical arrays, you can use relational operations to test for
equality and perform element-wise comparisons that have a meaningful mathematical
ordering.
Reduce Memory Requirements
This example shows how to compare the memory required to store data as a cell array of
character vectors versus a categorical array. Categorical arrays have categories that are
Advantages of Using Categorical Arrays