Documentation Center

  • Trial Software
  • Product Updates

mergelevels

Class: categorical

Merge levels

The nominal and ordinal array data types might be removed in a future release. To represent ordered and unordered discrete, nonnumeric data, use the MATLAB® categorical data type instead.

Syntax

B = mergelevels(A,oldlevels,newlevel)
B = mergelevels(A,oldlevels)

Description

B = mergelevels(A,oldlevels,newlevel) merges two or more levels of the categorical array A into a single new level. oldlevels is a cell array of strings or a 2-D character matrix that specifies the levels to be merged. Any elements of A that have levels in oldlevels are assigned the new level in the corresponding elements of B. newlevel is a character string that specifies the label for the new level. For ordinal arrays, the levels of A specified by oldlevels must be consecutive, and mergelevels inserts the new level to preserve the order of the levels.

B = mergelevels(A,oldlevels) merges two or more levels of A. For nominal arrays, mergelevels uses the first label in oldlevels as the label for the new level. For ordinal arrays, mergelevels uses the label corresponding to the lowest level in oldlevels as the label for the new level.

Examples

expand all

Merge Levels in Nominal Array

Load the sample data. Create a nominal array containing the iris species data.

load fisheriris;

The data contains length and width measurements from the sepals and petals of three species of iris flowers.

Create a nominal array containing the iris species data. Display the levels in the nominal array.

species = nominal(species);
getlevels(species)
ans = 

     setosa      versicolor      virginica 

Merge the 'setosa' and 'virginica' levels into a single new level called 'parent'.

species = mergelevels(species,...
                      {'setosa','virginica'},'parent');

Display the new level labels.

getlabels(species)
ans = 
    'parent'    'versicolor'

Merge Levels in Ordinal Array

Create an ordinal array.

A = ordinal([1 2 3 2 1],{'lo','med','hi'})
A = 
     lo      med      hi      med      lo

Merge the 'lo' and 'med' levels into a single new level labeled 'bad'.

A = mergelevels(A,{'lo','med'},'bad')
A = 
     bad      bad      hi      bad      bad 

Any element previous belonging to the levels 'lo' or 'med' now belongs to the level 'bad'.

See Also

| | | |

Was this topic helpful?