## Reorder Category Levels

### Note

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

### Reorder Category Levels in Ordinal Arrays

This example shows how to reorder the category levels in an ordinal array using `reorderlevels`.

```AllSizes = {'medium','large','small','small','medium',... 'large','medium','small'};```

The created variable, `AllSizes`, is a cell array of character vectors containing size measurements on eight objects.

Create an ordinal array.

Convert `AllSizes` to an ordinal array without specifying the order of the category levels.

```size = ordinal(AllSizes); getlevels(size)```
```ans = 1x3 ordinal large medium small ```

By default, the categories are ordered by their labels in ascending alphabetical order, `large` < `medium` < `small`.

Compare elements.

Check whether or not the first object (which has size `medium`) is smaller than the second object (which has size `large`).

`size(1) < size(2)`
```ans = logical 0 ```

The logical value `0` indicates that the medium object is not smaller than the large object.

Reorder category levels.

Reorder the category levels so that `small` < `medium` < `large`.

```size = reorderlevels(size,{'small','medium','large'}); getlevels(size)```
```ans = 1x3 ordinal small medium large ```

Compare elements.

Verify that the first object is now smaller than the second object.

`size(1) < size(2)`
```ans = logical 1 ```

The logical value `1` indicates that the expected inequality now holds.

### Reorder Category Levels in Nominal Arrays

This example shows how to reorder the category levels in nominal arrays using `reorderlevels`. By definition, nominal array categories have no natural ordering. However, you might want to change the order of levels for display or analysis purposes. For example, when fitting a regression model with categorical covariates, `fitlm` uses the first level of a nominal independent variable as the reference category.

The dataset array, `hospital`, contains variables measured on 100 sample patients. The variable `Weight` contains the weight of each patient. The variable `Sex` is a nominal variable containing the gender, `Male` or `Female`, for each patient.

```load hospital getlevels(hospital.Sex)```
```ans = 1x2 nominal Female Male ```

By default, the order of the nominal categories is in ascending alphabetical order of the labels.

Plot data grouped by category level.

Draw box plots of weight, grouped by gender.

```figure boxplot(hospital.Weight,hospital.Sex) title('Weight by Gender')``` The box plots appear in the same alphabetical order returned by `getlevels`.

Change the category order.

Change the order of the category levels.

```hospital.Sex = reorderlevels(hospital.Sex,{'Male','Female'}); getlevels(hospital.Sex)```
```ans = 1x2 nominal Male Female ```

The levels are in the newly specified order.

Plot data in new order.

Draw box plots of weight by gender.

```figure boxplot(hospital.Weight,hospital.Sex) title('Weight by Gender')``` The order of the box plots corresponds to the new level order.