MATLAB Examples

Reorder Category Levels in Ordinal Arrays

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

Contents

Load sample data.

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 array

     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 array

     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.