Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

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

collapse 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

collapse all

A — Categorical arrayvector | matrix | multidimensional array

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

newcats — New categoriescharacter vector | cell array of character vectors

New categories, specified as a character vector or a cell array of character vectors.

beforewhere — Category to precedecharacter vector

Category to precede, specified as a character vector.

afterwhere — Category to followcharacter vector

Category to follow, specified as a character vector.

Introduced in R2013b

Was this topic helpful?