# removecats

Remove categories from categorical array

## Description

example

B = removecats(A) removes unused categories from the categorical array, A. The output categorical array, B, has the same size and values as A. However, B might have fewer categories.

example

B = removecats(A,oldcats) removes the categories specified by oldcats. The function removes categories but does not remove any elements of the array. Therefore the elements of B are undefined where the corresponding elements of A belong to any category specified by oldcats.

## Examples

collapse all

Create a categorical array representing political parties of four people.

A = categorical({'republican' 'democrat' 'democrat' 'republican'},...
{'democrat' 'republican' 'independent'})
A = 1x4 categorical
republican      democrat      democrat      republican

A is a 1-by-4 categorical array.

Summarize the categorical array, A.

summary(A)
democrat      republican      independent
2             2               0

A has three categories. democrat appears twice in the array, republican appears twice in the array, and independent is unused.

Remove the unused category, independent.

B = removecats(A)
B = 1x4 categorical
republican      democrat      democrat      republican

B has the same values as A.

Display the categories of B.

categories(B)
ans = 2x1 cell
{'democrat'  }
{'republican'}

B has fewer categories than A.

Create a categorical array, A, containing modes of transportation.

A = categorical({'plane' 'car'; 'train' 'car'; 'plane' 'car'})
A = 3x2 categorical
plane      car
train      car
plane      car

A is a 3-by-2 categorical array.

Display the categories of A.

categories(A)
ans = 3x1 cell
{'car'  }
{'plane'}
{'train'}

A has three categories, car, plane, and train.

Remove the category, train.

B = removecats(A,'train')
B = 3x2 categorical
plane            car
<undefined>      car
plane            car

The element that was from the category train is now undefined.

Display the categories of B.

categories(B)
ans = 2x1 cell
{'car'  }
{'plane'}

B has one fewer category than A.

Create a categorical array. This array has many different categories that can stand for "yes" and "no".

C = categorical(["Y","Yes","N","No","Yes","Y"])
C = 1x6 categorical
Y      Yes      N      No      Yes      Y

categories(C)
ans = 4x1 cell
{'N'  }
{'No' }
{'Y'  }
{'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.

Remove the categories whose names start with Y. The removecats function removes categories but does not remove any elements of the input array. Therefore elements that belonged to the categories whose names started with Y are now undefined values.

C = removecats(C,"Y"+wildcardPattern)
C = 1x6 categorical
Columns 1 through 5

<undefined>      <undefined>      N      No      <undefined>

Column 6

<undefined>

categories(C)
ans = 2x1 cell
{'N' }
{'No'}

## Input Arguments

collapse all

Input array, specified as a categorical array.

Categories to remove, specified as a string array, character vector, cell array of character vectors, or pattern scalar. The default is all the unused categories from A.

## Tips

• ~ismember(categories(A),unique(A)) returns logical 1 (true) for any unused category of A.

## Version History

Introduced in R2013b