Quantcast

Documentation Center

  • Trial Software
  • Product Updates

ordinal

Class: ordinal

Construct ordinal categorical array

The nominal and ordinal array data types might be removed in a future release. To represent ordered and unordered discrete, nonnumeric data, use the MATLAB® categorical data type instead.

Syntax

B = ordinal(A)
B = ordinal(A,labels)
B = ordinal(A,labels,levels)
B = ordinal(A,labels,[],edges)

Description

B = ordinal(A) creates an ordinal array B from the array A. A can be numeric, logical, character, categorical, or a cell array of strings. ordinal creates the levels of B from the sorted unique values in A, and creates default labels for them.

B = ordinal(A,labels) labels the levels in B using the character array or cell array of strings labels. ordinal assigns labels to levels in B in order according to the sorted unique values in A.

B = ordinal(A,labels,levels) creates an ordinal array with possible levels defined by levels. levels is a vector whose values can be compared to those in A using the equality operator. ordinal assigns labels to each level from the corresponding elements of labels. If A contains any values not present in levels, the levels of the corresponding elements of B are undefined. Use [] for labels to allow ordinal to create default labels.

B = ordinal(A,labels,[],edges) creates an ordinal array by binning the numeric array A, with bin edges given by the numeric vector edges. The uppermost bin includes values equal to the right-most edge. ordinal assigns labels to each level in B from the corresponding elements of labels. edges must have one more element than labels.

By default, an element of B is undefined if the corresponding element of A is NaN (when A is numeric), an empty string (when A is character), or undefined (when A is categorical). ordinal treats such elements as "undefined" or "missing" and does not include entries for them among the possible levels for B. To create an explicit level for such elements instead of treating them as undefined, you must use the levels input, and include NaN, the empty string, or an undefined element.

You may include duplicate labels in labels in order to merge multiple values in A into a single level in B.

Examples

expand all

Create and Label Ordinal Arrays

Create an ordinal array from integer data, and provide explicit labels.

quality1 = ordinal([1 2 3; 3 2 1; 2 1 3],...
   {'low' 'medium' 'high'})
quality1 = 

     low         medium      high 
     high        medium      low  
     medium      low         high 

Create an ordinal array from integer data, and provide both explicit labels and an explicit order.

quality2 = ordinal([1 2 3; 3 2 1; 2 1 3],...
   {'high' 'medium' 'low'},[3 2 1])
quality2 = 

     low         medium      high 
     high        medium      low  
     medium      low         high 

Create an ordinal array by binning floating point values.

size = ordinal(rand(5,2),{'small' 'medium' 'large'},...
   [],[0 1/3 2/3 1])
size = 

     small       small  
     large       medium 
     large       large  
     medium      large  
     large       large 

Create Ordinal Array Using Sample Data

Load the sample data.

load fisheriris;

Create an ordinal array from the measurements in Fisher's iris data, ignoring decimal lengths.

m = floor(min(meas(:)));
M = floor(max(meas(:)));
labels = num2str((m:M)');
edges = m:M+1;
cms = ordinal(meas,labels,[],edges);

Display the first five rows of meas, which contains actual flower measurements.

meas(1:5,:)
ans =

    5.1000    3.5000    1.4000    0.2000
    4.9000    3.0000    1.4000    0.2000
    4.7000    3.2000    1.3000    0.2000
    4.6000    3.1000    1.5000    0.2000
    5.0000    3.6000    1.4000    0.2000

Display the first five rows of the ordinal array cms.

cms(1:5,:)
ans = 

     5      3      1      0 
     4      3      1      0 
     4      3      1      0 
     4      3      1      0 
     5      3      1      0 

Create Groups in Ordinal Array

Load the sample data.

load hospital;

Create an age group ordinal array from the patients' age data.

edges = 0:10:100;
labels = strcat(num2str((0:10:90)','%d'),{'s'});
AgeGroup = ordinal(hospital.Age,labels,[],edges);

Display the first five elements of the ordinal array AgeGroup.

AgeGroup(1:5)
ans = 

     30s 
     40s 
     30s 
     40s 
     40s 

See Also

|

Was this topic helpful?