Convert to an Ordinal Categorical Array
Add a mathematical ordering to the categories in colors. Specify the category order that represents
the ordering of color spectrum, red < green < blue.
colors = categorical(colors,{'red','green' 'blue'},'Ordinal',true)
colors = 2x4 categorical
blue
red
green
blue
blue
green
green
blue
The elements in the categorical array remain the same.
List the discrete categories in colors.
categories(colors)
ans = 3x1 cell
{'red' }
{'green'}
{'blue' }
Compare Elements Based on Order
Determine if elements in the first column of colors are greater than the elements in the second
column.
colors(:,1) > colors(:,2)
ans = 2x1 logical array
1
1
Both values in the first column, blue, are greater than the corresponding values in the second
column, red and green.
Find all the elements in colors that are less than 'blue'.
colors < 'blue'
ans = 2x4 logical array
0
1
1
0
0
1
1
0
The function lt (<) indicates the location of all green and red values with 1.
See Also
categorical | categories
Compare Categorical Array Elements
8-17
Add a mathematical ordering to the categories in colors. Specify the category order that represents
the ordering of color spectrum, red < green < blue.
colors = categorical(colors,{'red','green' 'blue'},'Ordinal',true)
colors = 2x4 categorical
blue
red
green
blue
blue
green
green
blue
The elements in the categorical array remain the same.
List the discrete categories in colors.
categories(colors)
ans = 3x1 cell
{'red' }
{'green'}
{'blue' }
Compare Elements Based on Order
Determine if elements in the first column of colors are greater than the elements in the second
column.
colors(:,1) > colors(:,2)
ans = 2x1 logical array
1
1
Both values in the first column, blue, are greater than the corresponding values in the second
column, red and green.
Find all the elements in colors that are less than 'blue'.
colors < 'blue'
ans = 2x4 logical array
0
1
1
0
0
1
1
0
The function lt (<) indicates the location of all green and red values with 1.
See Also
categorical | categories
Compare Categorical Array Elements
8-17
