Documentation Center

  • Trials
  • Product Updates

addcats

Add categories to categorical array

Syntax

  • B = addcats(A,newcats) example
  • B = addcats(A,newcats,'Before',beforewhere) example
  • B = addcats(A,newcats,'After',afterwhere)

Description

example

B = addcats(A,newcats) adds categories to the end of the category list for the input categorical array, A. The output categorical array, B, contains the same values as A. The output, B, does not contain any elements equal to the new categories until you assign values from newcats to elements in B.

If A is an ordinal categorical array, you must specify the 'Before',beforewhere or 'After',afterwhere input arguments.

example

B = addcats(A,newcats,'Before',beforewhere) adds categories before the category specified by beforewhere.

B = addcats(A,newcats,'After',afterwhere) adds categories after the category specified by afterwhere.

Examples

expand all

Add Categories at End

Create a nonordinal categorical array.

A = categorical({'republican' 'democrat' 'republican';...
    'democrat' 'republican' 'democrat'})
A = 

     republican      democrat        republican 
     democrat        republican      democrat   

Display the categories of A.

categories(A)
ans = 

    'democrat'
    'republican'

A is a 2-by-3 categorical array with two categories.

Add the categories, independent and undeclared, to the end of the category list

B = addcats(A,{'independent' 'undeclared'})
B = 

     republican      democrat        republican 
     democrat        republican      democrat   

B contains the same values as A.

Display the categories of B.

categories(B)
ans = 

    'democrat'
    'republican'
    'independent'
    'undeclared'

B is a 2-by-3 categorical array with four categories.

Add Categories and Specify Category to Precede

Create an ordinal categorical array.

A = categorical({'medium' 'large'; 'small' 'xlarge'; 'large' 'medium'},...
    {'small' 'medium' 'large' 'xlarge'},'Ordinal',true)
A = 

     medium      large  
     small       xlarge 
     large       medium 

Display the categories of A.

categories(A)
ans = 

    'small'
    'medium'
    'large'
    'xlarge'

Since A is ordinal, the categories have the mathematical ordering small < medium < large < xlarge.

Add the category xsmall before small.

B = addcats(A,'xsmall','Before','small')
B = 

     medium      large  
     small       xlarge 
     large       medium 

B contains the same values as A.

Display the categories of B.

categories(B)
ans = 

    'xsmall'
    'small'
    'medium'
    'large'
    'xlarge'

The categories have the mathematical ordering xsmall < small < medium < large < xlarge.

Input Arguments

expand all

A — Categorical arrayvector | matrix | multidimensional array

Categorical array, specified as a vector, matrix, or multidimensional array.

newcats — New categoriesstring | cell array of strings

New categories, specified as a string or a cell array of strings.

beforewhere — Category to precedestring

Category to precede, specified as a string.

afterwhere — Category to followstring

Category to follow, specified as a string.

See Also

| | | | |

Was this topic helpful?