Main Content


(Not Recommended) Merge levels of nominal or ordinal arrays

The nominal and ordinal array data types are not recommended. To represent ordered and unordered discrete, nonnumeric data, use the Categorical Arrays data type instead.


B = mergelevels(A,oldlevels) merges two or more levels of A.

  • If A is a nominal array, mergelevels uses the first label in oldlevels as the new level.

  • If A is an ordinal array, the levels specified by oldlevels must be consecutive, and mergelevels uses the label corresponding to the lowest level in oldlevels as the label for the new level.


B = mergelevels(A,oldlevels,newlevel) merges two or more levels into the new level with label newlevel.


collapse all

Create a nominal array from data in a cell array.

colors = nominal({'r','b','g';'g','r','b';'b','r','g'},...
colors = 3x3 nominal
     red        blue      green 
     green      red       blue  
     blue       red       green 

Merge the elements of the 'red' and 'blue' levels into a new level labeled 'purple'.

colors = mergelevels(colors,{'red','blue'},'purple')
colors = 3x3 nominal
     purple      purple      green  
     green       purple      purple 
     purple      purple      green  

Display the levels of colors.

ans = 1x2 nominal
     purple      green 

Input Arguments

collapse all

Nominal or ordinal array, specified as a nominal or ordinal array object created with nominal or ordinal.

Levels to merge, specified as a string array, cell array of character vectors, or 2-D character array. For ordinal arrays, the levels in oldlevels must be consecutive.

Data Types: char | string | cell

Level to create from the merged levels, specified as a character vector or string scalar that gives the label for the new level.

Data Types: char | string

Output Arguments

collapse all

Nominal or ordinal array, returned as a nominal or ordinal array object.

Version History

Introduced in R2007a