Merge categories in categorical array
b =mergecats(A,oldcats)
b =mergecats(A,oldcats,newcat)
example
b =mergecats(A,oldcats)merges two or more categories inAinto the first category,oldcats(1). Any values inAfromoldcatsbecomeoldcats(1)inB.
A
oldcats
oldcats(1)
B
b =mergecats(A,oldcats,newcat)mergesoldcatsinto a single new category,newcat. Any values inAfromoldcatsbecomenewcatinB.
newcat
collapse all
Create a categorical array containing various colors.
A = categorical({'red';'blue';'pink';'red';'blue';'red'})
A =6×1 categorical arrayred blue pink red blue red
Ais a 6-by-1 categorical array.
Display the categories ofA.
categories(A)
ans =3×1 cell array'blue' 'pink' 'red'
The three categories are in alphabetical order.
合并类别red和pinkinto the categoryred. Specifyredfirst inoldcatsto use it as the merged category.
red
pink
oldcats = {'red','pink'}; B = mergecats(A,oldcats)
b =6×1 categorical arrayred blue red red blue red
mergecatsreplaces the valuepinkfromA(3)withred.
mergecats
A(3)
Display the categories ofB.
categories(B)
ans =2×1 cell array'blue' 'red'
Bhas two categories instead of three.
Create a categorical array containing various items.
A = categorical({'shirt''pants';'shoes''shirt';'dress''belt'})
A =3×2分类阵列shirt pants shoes shirt dress belt
ans =5×1 cell array'belt' 'dress' 'pants' 'shirt' 'shoes'
The five categories are in alphabetical order.
合并类别belt和shoesinto a new category calledother.
belt
shoes
other
b =mergecats(A,{'belt''shoes'},'other')
b =3×2分类阵列衬衫裤子其他衬衫连衣裙其他
The valueotherreplaces all instances ofbelt和shoes.
ans =4×1 cell array'other' 'dress' 'pants' 'shirt'
Bhas four categories and the order is no longer alphabetical.otherappears in place ofbelt.
创建一个顺序的分类阵列。
A = categorical([1 2 3 2 1],1:3,{'poor','公平的','good'},'Ordinal',真正的)
A =1×5分类阵列poor fair good fair poor
ans =3×1 cell array'poor' 'fair' 'good'
自从Ais ordinal, the categories have the mathematical orderingpoor < fair < good.
poor < fair < good
Consider allfair或者poorvalues to be坏. SinceAis ordinal, the categories to merge must be consecutive.
fair
poor
坏
b =mergecats(A,{'公平的''poor'},'bad')
b =1×5分类阵列坏坏good bad bad
The value坏replaces all instances offair和poor.
ans =2×1 cell array'bad' 'good'
Bhas two categories with the mathematical ordering:坏< good.
坏< good
Categorical array, specified as a vector, matrix, or multidimensional array.
Categories to merge, specified as a cell array of character vectors. IfAis ordinal, the categories to merge must be consecutive.
New category, specified as a character vector.
This function fully supports tall arrays. For more information, seeTall Arrays.
addcats|categories|iscategory|removecats|renamecats|reordercats|setcats
addcats
categories
iscategory
removecats
renamecats
reordercats
setcats
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.
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:
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Contact your local office
Get trial now