MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

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 array
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 array
{'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.

8 Categorical Arrays

Free download pdf