MATLAB Examples

# Create Ordinal Arrays

This example shows how to create ordinal arrays using ordinal.

## Contents

```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.

Create an ordinal array with category levels and labels corresponding to the values in the cell array (the default levels and labels).

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

## Explore category labels.

By default, ordinal uses the original character vectors as category labels. The default order of the categories is ascending alphabetical order.

```getlabels(sizeOrd) ```
```ans = 1x3 cell array {'large'} {'medium'} {'small'} ```

Suppose that you want to include additional levels for the ordinal array, xsmall and xlarge, even though they do not occur in the original data. To specify additional levels, use the third input argument to ordinal.

```sizeOrd2 = ordinal(AllSizes,{},... {'xsmall','small','medium','large','xlarge'}); getlevels(sizeOrd2) ```
```ans = 1x5 ordinal array xsmall small medium large xlarge ```

## Explore category labels.

To see which levels are actually present in the data, use unique.

```unique(sizeOrd2) ```
```ans = 1x3 ordinal array small medium large ```

## Specify the category order.

Convert AllSizes to an ordinal array with categories small < medium < large. Generally, an ordinal array is distinct from a nominal array because there is a natural ordering for levels of an ordinal array. You can use the third input argument to ordinal to specify the ascending order of the levels. Here, the order of the levels is smallest to largest.

```sizeOrd = ordinal(AllSizes,{},{'small','medium','large'}); getlevels(sizeOrd) ```
```ans = 1x3 ordinal array small medium large ```

The second input argument for ordinal is a list of labels for the category levels. When you use braces {} for the level labels, ordinal uses the labels specified in the third input argument (the labels come from the levels present in the data if only one input argument is used).

## Compare elements.

Verify that the first object (with size medium) is smaller than the second object (with size large).

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

The logical value 1 indicates that the inequality holds.