Sort Ordinal Arrays

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.

Sort Ordinal Arrays

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

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.

See Also

Related Examples

More About