reordercats
Reorder categories in categorical
array
Description
B = reordercats(
reorders the categories in the
A
)categorical
array, A
, to be in
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
Alphabetize Categories of Nonordinal Categorical Array
Create two categorical arrays, X
and Y
.
X = categorical({'frog';'cat';'cat';'ant';'frog'})
X = 5x1 categorical
frog
cat
cat
ant
frog
Y = categorical({'deer';'bear';'eagle';'deer'})
Y = 4x1 categorical
deer
bear
eagle
deer
X
is a 5-by-1 categorical array. The categories of X
are the sorted unique values from the array: {'ant';'cat';'frog'}
.
Y
is a 4-by-1 categorical array. The categories of Y
are the sorted unique values from the array: {'bear';'deer';'eagle'}
.
Concatenate X
and Y
into a single categorical array, A
.
A = [X;Y]
A = 9x1 categorical
frog
cat
cat
ant
frog
deer
bear
eagle
deer
vertcat
appends the values from Y
to the values from X
.
List the categories of the categorical array, A
.
acats = categories(A)
acats = 6x1 cell
{'ant' }
{'cat' }
{'frog' }
{'bear' }
{'deer' }
{'eagle'}
vertcat
appends the categories of Y
to the categories from X
. The categories of A
are not in alphabetical order.
Reorder the categories of A
into alphabetical order.
B = reordercats(A)
B = 9x1 categorical
frog
cat
cat
ant
frog
deer
bear
eagle
deer
The output categorical array, B
, has the same elements in the same order as the input categorical array, A
.
List the categories of the categorical array, B
.
bcats = categories(B)
bcats = 6x1 cell
{'ant' }
{'bear' }
{'cat' }
{'deer' }
{'eagle'}
{'frog' }
The categories of B
are in alphabetical order.
Reorder Categories in Nonordinal Categorical Array
Create a categorical array containing the color of various items.
A = categorical({'red';'green';'blue';'red';'green';'red';'blue';'blue'})
A = 8x1 categorical
red
green
blue
red
green
red
blue
blue
A
is an 8-by-1 categorical array.
Display the categories of A
.
categories(A)
ans = 3x1 cell
{'blue' }
{'green'}
{'red' }
The categories of A
are in alphabetical order and have no mathematical meaning.
Reorder the categories to match the order commonly used for colors.
B = reordercats(A,{'red','green','blue'})
B = 8x1 categorical
red
green
blue
red
green
red
blue
blue
B
contains the same values as A
.
Display the categories of B
.
categories(B)
ans = 3x1 cell
{'red' }
{'green'}
{'blue' }
B
is not ordinal and the order of the categories has no mathematical meaning. Although 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, A
, containing modes of transportation. Order the categories based on the average price of travel.
A = categorical({'plane';'car'; 'train';'car';'plane';'car'},... {'car','train','plane'},'Ordinal',true)
A = 6x1 categorical
plane
car
train
car
plane
car
A
is a 6-by-1 ordinal categorical array.
Display the categories of A
.
categories(A)
ans = 3x1 cell
{'car' }
{'train'}
{'plane'}
Since A
is ordinal, car < train < plane
.
Reorder the categories to reflect a decrease in the cost of train travel.
B = reordercats(A,{'train','car','plane'})
B = 6x1 categorical
plane
car
train
car
plane
car
B
contains the same values as A
.
Display the categories of B
.
categories(B)
ans = 3x1 cell
{'train'}
{'car' }
{'plane'}
The mathematical ordering of the categories is now train < car < plane
. The results from relational operations, min
, and max
reflect the new category ordering.
Reorder Categories with Numeric Vector
Create a categorical array, A
, containing modes of transportation.
A = categorical({'plane';'car';'train';'car';'car';'plane';'car'})
A = 7x1 categorical
plane
car
train
car
car
plane
car
Display the categories of A
.
categories(A)
ans = 3x1 cell
{'car' }
{'plane'}
{'train'}
Reorder categories from least to most frequent occurrence in A
.
B = countcats(A); [C,neworder] = sort(B); neworder
neworder = 3×1
3
2
1
D = reordercats(A,neworder); categories(D)
ans = 3x1 cell
{'train'}
{'plane'}
{'car' }
Because countcats
counts the occurrences of each category, neworder
describes how to reorder the categories—not the elements—of A
.
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 for B
string array | cell array of character vectors | numeric vector | pattern
array
New category order for B
, specified as a string array, cell array of
character vectors, numeric vector, or pattern
array. neworder
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.
Usage notes and limitations:
The
neworder
input argument does not support pattern expressions.
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
Ouvrir l'exemple
Vous possédez une version modifiée de cet exemple. Souhaitez-vous ouvrir cet exemple avec vos modifications ?
Commande MATLAB
Vous avez cliqué sur un lien qui correspond à cette commande MATLAB :
Pour exécuter la commande, saisissez-la dans la fenêtre de commande de MATLAB. Les navigateurs web ne supportent pas les commandes MATLAB.
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)