mergecats
Merge categories in categorical
array
Description
Examples
Merge Two Categories into One
Create a categorical array containing various colors.
A = categorical({'red';'blue';'pink';'red';'blue';'red'})
A = 6x1 categorical
red
blue
pink
red
blue
red
A
is a 6-by-1 categorical array.
Display the categories of A
.
categories(A)
ans = 3x1 cell
{'blue'}
{'pink'}
{'red' }
The three categories are in alphabetical order.
Merge the categories red
and pink
into the category red
. Specify red
first in oldcats
to use it as the merged category.
oldcats = {'red','pink'}; B = mergecats(A,oldcats)
B = 6x1 categorical
red
blue
red
red
blue
red
mergecats
replaces the value pink
from A(3)
with red
.
Display the categories of B
.
categories(B)
ans = 2x1 cell
{'blue'}
{'red' }
B
has two categories instead of three.
Merge Alphabetically Listed Categories
Create a categorical array containing various items.
A = categorical({'shirt' 'pants'; 'shoes' 'shirt'; 'dress' 'belt'})
A = 3x2 categorical
shirt pants
shoes shirt
dress belt
Display the categories of A
.
categories(A)
ans = 5x1 cell
{'belt' }
{'dress'}
{'pants'}
{'shirt'}
{'shoes'}
The five categories are in alphabetical order.
Merge the categories belt
and shoes
into a new category called other
.
B = mergecats(A,{'belt' 'shoes'},'other')
B = 3x2 categorical
shirt pants
other shirt
dress other
The value other
replaces all instances of belt
and shoes
.
Display the categories of B
.
categories(B)
ans = 4x1 cell
{'other'}
{'dress'}
{'pants'}
{'shirt'}
B
has four categories and the order is no longer alphabetical. other
appears in place of belt
.
Merge Categories of Ordinal Categorical Array
Create an ordinal categorical array.
A = categorical([1 2 3 2 1],1:3,{'poor','fair','good'},'Ordinal',true)
A = 1x5 categorical
poor fair good fair poor
Display the categories of A
.
categories(A)
ans = 3x1 cell
{'poor'}
{'fair'}
{'good'}
Since A
is ordinal, the categories have the mathematical ordering poor < fair < good
.
Consider all fair
or poor
values to be bad
. Since A
is ordinal, the categories to merge must be consecutive.
B = mergecats(A,{'fair' 'poor'},'bad')
B = 1x5 categorical
bad bad good bad bad
The value bad
replaces all instances of fair
and poor
.
Display the categories of B
.
categories(B)
ans = 2x1 cell
{'bad' }
{'good'}
B
has two categories with the mathematical ordering: bad < good
.
Specify Categories by Using Pattern
Create a categorical
array. This array has many different categories that can stand for "yes" and "no".
C = categorical(["Y" "Yes" "Yeah" "N" "No" "Nope"])
C = 1x6 categorical
Y Yes Yeah N No Nope
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.
Merge all categories whose names start with Y
into one category named yes
. Then merge all the categories whose names start with N
into one category named no
. As a result, values that have the same meaning are all in the same category. Now, C
has only two categories.
C = mergecats(C,"Y" + wildcardPattern,"yes"); C = mergecats(C,"N" + wildcardPattern,"no")
C = 1x6 categorical
yes yes yes no no no
categories(C)
ans = 2x1 cell
{'no' }
{'yes'}
Input Arguments
A
— Input array
categorical
array
Input array, specified as a categorical
array.
oldcats
— Categories to merge
string array | cell array of character vectors | pattern
scalar
Categories to merge, specified as a string array, cell array of character vectors, or pattern
scalar. If A
is ordinal, then the
categories to merge must be consecutive.
newcat
— New category
string scalar | character vector
New category, specified as a string scalar or a character vector.
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
Usage notes and limitations:
The
oldcats
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
oldcats
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
| renamecats
| reordercats
| setcats
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
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 bat365 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)