Combine Categorical Arrays Using Multiplication
This example shows how to use the times function to combine categorical arrays, including ordinal
categorical arrays and arrays with undefined elements. When you call times on 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. times
forms 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 using times. 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
blue +
red -
green +
Cartesian Product of Categories
Show the categories of C. The categories are all the ordered pairs that can be created from the
categories of A and B, also known as the Cartesian product.
categories(C)
ans = 6x1 cell
{'blue +' }
{'blue -' }
{'green +'}
{'green -'}
{'red +' }
{'red -' }
As a consequence, A.*B does not equal B.*A.
D = B.*A
D = 1x3 categorical
+ blue
- red
+ green
categories(D)
ans = 6x1 cell
{'+ blue' }
{'+ green'}
{'+ red' }
{'- blue' }
{'- green'}
{'- red' }
8 Categorical Arrays
8-22
This example shows how to use the times function to combine categorical arrays, including ordinal
categorical arrays and arrays with undefined elements. When you call times on 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. times
forms 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 using times. 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
blue +
red -
green +
Cartesian Product of Categories
Show the categories of C. The categories are all the ordered pairs that can be created from the
categories of A and B, also known as the Cartesian product.
categories(C)
ans = 6x1 cell
{'blue +' }
{'blue -' }
{'green +'}
{'green -'}
{'red +' }
{'red -' }
As a consequence, A.*B does not equal B.*A.
D = B.*A
D = 1x3 categorical
+ blue
- red
+ green
categories(D)
ans = 6x1 cell
{'+ blue' }
{'+ green'}
{'+ red' }
{'- blue' }
{'- green'}
{'- red' }
8 Categorical Arrays
8-22
