MATLAB Examples

Sort Ordinal Arrays

This example shows how to determine sorting order for ordinal arrays.

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 with levels small < medium < large.

AllSizes = ordinal(AllSizes,{},{'small','medium','large'});
getlevels(AllSizes)
ans = 

  1x3 ordinal array

     small      medium      large 

Sort the ordinal array.

When you sort ordinal arrays, the sorted observations are in the same order as the category levels.

sizeSort = sort(AllSizes);
sizeSort(:)
ans = 

  8x1 ordinal array

     small 
     small 
     small 
     medium 
     medium 
     medium 
     large 
     large 

The sorted ordinal array, sizeSort, contains the observations ordered from small to large.