Documentation

Combine Categorical Arrays Using Multiplication

This example shows how to use thetimesfunction to combine categorical arrays, including ordinal categorical arrays and arrays with undefined elements. When you calltimeson two categorical arrays, the output is a categorical array with new categories. The set of new categories is the set of all the ordered pairs created from the categories of the input arrays, or the Cartesian product.timesforms each element of the output array as the ordered pair of the corresponding elements of the input arrays. The output array has the same size as the input arrays.

Combine Two Categorical Arrays

Combine two categorical arrays usingtimes。The input arrays must have the same number of elements, but can have different numbers of categories.

A = categorical({'blue','red','green'}); B = categorical({'+','-','+'}); C = A.*B
C =1x3 categorical arrayblue + red - green +

Cartesian Product of Categories

Show the categories ofC。类别的所有命令对can be created from the categories ofAandB, also known as the Cartesian product.

categories(C)
ans =6x1 cell array{'blue +' } {'blue -' } {'green +'} {'green -'} {'red +' } {'red -' }

As a consequence,A.*Bdoes not equalB.*A

D = B.*A
D =1x3 categorical array+ blue - red + green
categories(D)
ans =6x1 cell array{'+ blue' } {'+ green'} {'+ red' } {'- blue' } {'- green'} {'- red' }

Multiplication with Undefined Elements

Combine two categorical arrays. If eitherAorBhave an undefined element, the corresponding element ofCisundefined

A = categorical({'blue','red','green','black'}); B = categorical({'+','-','+','-'}); A = removecats(A,{'black'}); C = A.*B
C =1x4 categorical arrayblue + red - green + 

Cartesian Product of Ordinal Categorical Arrays

Combine two ordinal categorical arrays.Cis an ordinal categorical array only ifAandBare both ordinal. The ordering of the categories ofCfollows from the orderings of the input categorical arrays.

A = categorical({'blue','red','green'},{'green','red','blue'},'Ordinal',true); B = categorical({'+','-','+'},'Ordinal',true); C = A.*B; categories(C)
ans =6x1 cell array{'green +'} {'green -'} {'red +' } {'red -' } {'blue +' } {'blue -' }

See Also

|||

Related Examples

More About