Documentation

mergecats

Merge categories in categorical array

Syntax

b =mergecats(A,oldcats)
b =mergecats(A,oldcats,newcat)

Description

example

b =mergecats(A,oldcats)merges two or more categories inAinto the first category,oldcats(1). Any values inAfromoldcatsbecomeoldcats(1)inB.

example

b =mergecats(A,oldcats,newcat)mergesoldcatsinto a single new category,newcat. Any values inAfromoldcatsbecomenewcatinB.

Examples

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.

合并类别redpinkinto the categoryred. Specifyredfirst inoldcatsto use it as the merged category.

oldcats = {'red','pink'}; B = mergecats(A,oldcats)
b =6×1 categorical arrayred blue red red blue red

mergecatsreplaces the valuepinkfromA(3)withred.

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

Display the categories ofA.

categories(A)
ans =5×1 cell array'belt' 'dress' 'pants' 'shirt' 'shoes'

The five categories are in alphabetical order.

合并类别beltshoesinto a new category calledother.

b =mergecats(A,{'belt''shoes'},'other')
b =3×2分类阵列衬衫裤子其他衬衫连衣裙其他

The valueotherreplaces all instances ofbeltshoes.

Display the categories ofB.

categories(B)
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

Display the categories ofA.

categories(A)
ans =3×1 cell array'poor' 'fair' 'good'

自从Ais ordinal, the categories have the mathematical orderingpoor < fair < good.

Consider allfair或者poorvalues to be. SinceAis ordinal, the categories to merge must be consecutive.

b =mergecats(A,{'公平的''poor'},'bad')
b =1×5分类阵列坏坏good bad bad

The valuereplaces all instances offairpoor.

Display the categories ofB.

categories(B)
ans =2×1 cell array'bad' 'good'

Bhas two categories with the mathematical ordering:坏< good.

Input Arguments

collapse all

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.

Extended Capabilities

Introduced in R2013b

Was this topic helpful?