renamecats
Rename categories in categorical array
Description
Examples
Rename All Categories
Create a categorical array containing states from New England.
A = categorical({'MA';'ME';'CT';'VT';'ME';'NH';'VT';'MA';'NH';'CT';'RI'})
A = 11x1 categorical
MA
ME
CT
VT
ME
NH
VT
MA
NH
CT
RI
A
is an 11-by-1 categorical array.
Display the categories of A
.
categories(A)
ans = 6x1 cell
{'CT'}
{'MA'}
{'ME'}
{'NH'}
{'RI'}
{'VT'}
A
has six categories.
Rename the categories to use the full state name instead of the abbreviation.
B = renamecats(A,{'Connecticut','Massachusetts',... 'Maine','New Hampshire','Rhode Island' 'Vermont'})
B = 11x1 categorical
Massachusetts
Maine
Connecticut
Vermont
Maine
New Hampshire
Vermont
Massachusetts
New Hampshire
Connecticut
Rhode Island
Elements of B
use the new category names.
Display the categories of B
.
categories(B)
ans = 6x1 cell
{'Connecticut' }
{'Massachusetts'}
{'Maine' }
{'New Hampshire'}
{'Rhode Island' }
{'Vermont' }
Rename One Category
Create a categorical array containing colors.
A = categorical({'red' 'blue'; 'purple' 'white'; 'green' 'red'})
A = 3x2 categorical
red blue
purple white
green red
A
is a 3-by-2 categorical array.
Display the categories of A
.
categories(A)
ans = 5x1 cell
{'blue' }
{'green' }
{'purple'}
{'red' }
{'white' }
A
has five categories that are listed in alphabetical order.
Change the category name from purple
to violet
.
B = renamecats(A,'purple','violet')
B = 3x2 categorical
red blue
violet white
green red
The element B(2,1)
is violet
instead of purple
.
Display the categories of B
.
categories(B)
ans = 5x1 cell
{'blue' }
{'green' }
{'violet'}
{'red' }
{'white' }
violet
replaces purple
and the categories are no longer in alphabetical order. Note that the category has not changed its position.
Input Arguments
A
— Categorical array
vector | matrix | multidimensional array
Categorical array, specified as a vector, matrix, or multidimensional array.
newnames
— New category names for B
character vector | cell array of character vectors | string array
New category names for B
, specified as a character vector, a cell array of
character vectors, or a string array. The new category names must be unique,
and must not duplicate any existing names.
oldnames
— Old category names from A
character vector | cell array of character vectors | string array
Old category names from A
, specified as a character vector, a cell array of
character vectors, or a string array.
Tips
Renaming categories does not change their position in
categories(B)
. Usereordercats
to change the category ordering. For example, you can useB = reordercats(B,sort(categories(B)))
to put the categories in alphabetical order.
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
This function fully supports tall arrays. For more information, see Tall Arrays.
C/C++ Code Generation
Generate C and C++ code using 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
| reordercats
| 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)