Products & Services Industries Academia Support User Community Company

Learn more about Statistics Toolbox   

ordinal - Class: ordinal

Construct ordinal categorical array

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

Example 1

Create an ordinal array with labels from random integer data:

x = floor(3*rand(1,1e3));
x(1:5)
ans =
     1     2     1     2     0

o = ordinal(x,{'I','II','III'}); 
o(1:5)
ans = 
     II    III    II    III    I

Example 2

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

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

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
cms(1:5,:)
ans = 
     5      3      1      0 
     4      3      1      0 
     4      3      1      0 
     4      3      1      0 
     5      3      1      0

Example 3

Create an age group ordinal array from the data in hospital.mat:

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

hospital.Age(1:5)
ans =
    38
    43
    38
    40
    49

AgeGroup(1:5)
ans = 
     30s 
     40s 
     30s 
     40s 
     40s 

See Also

histc, nominal

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS