Documentation

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 =2×4 categorical arrayblue red green blue blue green green blue

colorsis a 2-by-4 categorical array.

List the categories of the categorical array.

categories(colors)
ans =3×1 cell array'blue' 'green' 'red'

Determine If Elements Are Equal

Use the relational operator,eq(==), to compare the first and second rows ofcolors.

colors(1,:) == colors(2,:)
ans =1×4 logical array1 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 allbluevalues.

colors =='blue'
ans =2×4 logical array1 0 0 1 1 0 0 1

There are four blue entries incolors, one in each corner of the array.

Convert to an Ordinal Categorical Array

Add a mathematical ordering to the categories incolors. Specify the category order that represents the ordering of color spectrum,red < green < blue.

colors = categorical(colors,{'red','green''blue'},'Ordinal',真正的)
colors =2×4 categorical arrayblue red green blue blue green green blue

The elements in the categorical array remain the same.

List the discrete categories incolors.

categories(colors)
ans =3×1 cell array'red' 'green' 'blue'

Compare Elements Based on Order

Determine if elements in the first column ofcolorsare greater than the elements in the second column.

colors(:,1) > colors(:,2)
ans =2×1 logical array1

Both values in the first column,blue, are greater than the corresponding values in the second column,redandgreen.

Find all the elements incolorsthat are less than'blue'.

colors <'blue'
ans =2×4 logical array0 1 1 0 0 1 1 0

The functionlt(<) indicates the location of allgreenandredvalues with1.

See Also

|

Related Examples

More About

Was this topic helpful?