reordercats
Reorder categories in categorical array
Description
B = reordercats(
reorders the categories in a
categorical array. By default, A
)reordercats
uses alphanumeric
order.
The order of the categories is used by functions such as summary
and
histogram
. If the categorical array is ordinal, the order of
the categories defines their mathematical ordering. The first category specified is
the smallest and the last category is the largest.
Examples
Reorder Categories in Alphanumeric Order
When you create a categorical array, the categories always have a certain order. But if you add categories later, then the new set of categories might be out of order. To put categories in alphanumeric order, use the reordercats
function.
For example, concatenate two categorical arrays. Then reorder the categories of the result.
First create the arrays.
X = categorical(["Frog" "Cat" "Cat" "Ant" "Frog"])
X = 1x5 categorical
Frog Cat Cat Ant Frog
Y = categorical(["Deer" "Bear" "Eagle" "Deer"])
Y = 1x4 categorical
Deer Bear Eagle Deer
The categories of each array are in alphanumeric order.
Xcats = categories(X)
Xcats = 3x1 cell
{'Ant' }
{'Cat' }
{'Frog'}
Ycats = categories(Y)
Ycats = 3x1 cell
{'Bear' }
{'Deer' }
{'Eagle'}
Then concatenate X
and Y
into one categorical array.
A = [X Y]
A = 1x9 categorical
Frog Cat Cat Ant Frog Deer Bear Eagle Deer
List the categories. The combined set of categories is out of alphanumeric order. Concatenation simply appends one set of categories to the end of the other set.
Acats = categories(A)
Acats = 6x1 cell
{'Ant' }
{'Cat' }
{'Frog' }
{'Bear' }
{'Deer' }
{'Eagle'}
Reorder the categories. The output categorical array has the same elements as the input array.
B = reordercats(A)
B = 1x9 categorical
Frog Cat Cat Ant Frog Deer Bear Eagle Deer
List the categories. Now the combined set of categories is in alphanumeric order.
Bcats = categories(B)
Bcats = 6x1 cell
{'Ant' }
{'Bear' }
{'Cat' }
{'Deer' }
{'Eagle'}
{'Frog' }
Specify New Order for Categories
Create a categorical array.
A = categorical(["red" "green" "blue" "red" "green" "red" "blue" "blue"])
A = 1x8 categorical
red green blue red green red blue blue
Display the categories. They are in alphanumeric order.
categories(A)
ans = 3x1 cell
{'blue' }
{'green'}
{'red' }
Reorder the categories.
B = reordercats(A,["red" "green" "blue"])
B = 1x8 categorical
red green blue red green red blue blue
Display the categories. They are now in the RGB order commonly used for the color spectrum.
categories(B)
ans = 3x1 cell
{'red' }
{'green'}
{'blue' }
Because the array is not an ordinal categorical array, the order of the categories has no mathematical meaning. So, while the categories appear in the order of the color spectrum, relational operations, such as greater than and less than, have no meaning.
Reorder Categories in Ordinal Categorical Array
Create an ordinal categorical array that has modes of transportation. Order the categories based on the average cost of travel by each mode of transportation.
A = categorical(["plane" "car" "train" "car" "plane" "car"], ... ["car" "train" "plane"], ... Ordinal=true)
A = 1x6 categorical
plane car train car plane car
Display the categories. Because the array A
is ordinal, car < train < plane
.
categories(A)
ans = 3x1 cell
{'car' }
{'train'}
{'plane'}
For example, any element whose category is plane
or train
is greater than the category car
.
A(A > "car")
ans = 1x3 categorical
plane train plane
Reorder the categories to reflect a decrease in the cost of train travel.
B = reordercats(A,["train" "car" "plane"])
B = 1x6 categorical
plane car train car plane car
Display the categories. The mathematical ordering of the categories is now train < car < plane
.
categories(B)
ans = 3x1 cell
{'train'}
{'car' }
{'plane'}
For example, train
is no longer greater than car
. Results from relational operations, min
, and max
reflect the new category ordering.
B(B > "car")
ans = 1x2 categorical
plane plane
Reorder Categories with Numeric Vector
Create a categorical array that has modes of transportation.
A = categorical(["plane" "car" "train" "car" "car" "plane" "car"])
A = 1x7 categorical
plane car train car car plane car
Display the categories.
categories(A)
ans = 3x1 cell
{'car' }
{'plane'}
{'train'}
Count the number of times each category occurs in the array by using the countcats
function.
B = countcats(A)
B = 1×3
4 2 1
Create an order that goes from the category that occurs least frequently to the category that occurs most. To specify that order as a numeric vector, use the second output from the sort
function. The output neworder
describes how to reorder the categories—not the elements—of the categorical array.
[C,neworder] = sort(B); neworder
neworder = 1×3
3 2 1
Reorder categories from least to most frequent occurrence in the array.
D = reordercats(A,neworder); categories(D)
ans = 3x1 cell
{'train'}
{'plane'}
{'car' }
Specify Categories by Using Pattern
Create a categorical array. This array has many different categories that stand for "yes" and "no".
C = categorical(["Y" "Yes" "Yeah" "N" "No" "Nope"])
C = 1x6 categorical
Y Yes Yeah N No Nope
List the categories in order. By default, the sort order of these categories is alphabetical order, because MATLAB® stores characters as Unicode®.
categories(C)
ans = 6x1 cell
{'N' }
{'No' }
{'Nope'}
{'Y' }
{'Yeah'}
{'Yes' }
You can match multiple category names by using a pattern
. For example, to specify category names that start with a Y
, you can use a wildcard pattern. To create a wildcard pattern, use the wildcardPattern
function.
Reorder the categories. Change the sort order so that the categories that start with Y
come before the categories that start with N
.
C = reordercats(C,["Y"+wildcardPattern,"N"+wildcardPattern])
C = 1x6 categorical
Y Yes Yeah N No Nope
List the categories in their new order.
categories(C)
ans = 6x1 cell
{'Y' }
{'Yeah'}
{'Yes' }
{'N' }
{'No' }
{'Nope'}
Input Arguments
A
— Input array
categorical array
Input array, specified as a categorical array. If A
is
an ordinal categorical array, a reordering of the categories changes the
mathematical meaning. Consequently, the relational operators, such as
greater than and less than, might return different results.
neworder
— New category order
string array | cell array of character vectors | numeric vector | pattern
array
New category order, specified as a string array, cell array of character
vectors, numeric vector, or pattern
array. The new category order must be a permutation of
categories(A)
.
Tips
To convert the categorical array,
B
, to an ordinal categorical array, useB = categorical(B,Ordinal=true)
. You can specify the order of the categories withB = categorical(B,
, where the order of the values invalueset
,Ordinal=true)
defines the category order.valueset
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
The
reordercats
function fully supports tall arrays. For more information,
see Tall Arrays.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
The
neworder
input argument does not support pattern expressions.
For more information, see Code Generation for Categorical Arrays (MATLAB Coder).
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced in R2013b
See Also
categories
| addcats
| removecats
| iscategory
| mergecats
| renamecats
| setcats
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)