Compare Categorical Array Elements
This example shows how to use relational operations with a categorical array.
Create Categorical Array from Cell Array of Character Vectors
Create a 2-by-4 cell array of character vectors.
C = {'blue' 'red' 'green' 'blue';...
'blue' 'green' 'green' 'blue'};
colors = categorical(C)
colors = 2x4 categorical
blue
red
green
blue
blue
green
green
blue
colors is a 2-by-4 categorical array.
List the categories of the categorical array.
categories(colors)
ans = 3x1 cell
{'blue' }
{'green'}
{'red' }
Determine If Elements Are Equal
Use the relational operator, eq (==), to compare the first and second rows of colors.
colors(1,:) == colors(2,:)
ans = 1x4 logical array
1
0
1
1
Only the values in the second column differ between the rows.
Compare Entire Array to Character Vector
Compare the entire categorical array, colors, to the character vector 'blue' to find the location of
all blue values.
colors == 'blue'
ans = 2x4 logical array
1
0
0
1
1
0
0
1
There are four blue entries in colors, one in each corner of the array.
8 Categorical Arrays
8-16
Précédent

- 320/1450

Suivant