Main Content

Produce All Combinations of Categories from Two Categorical Arrays

This example shows how multiplication produces all combinations of the categories from two categorical arrays. When you multiply two categorical arrays, the output is a categorical array with entirely new categories. This set of new categories is the set of all the ordered pairs created from the categories of the input arrays. This set of all possible combinations of categories is also known as the Cartesian product of the two original sets of categories.

Multiplication also produces the elements of the output array from the corresponding elements of the input arrays. If either input array has undefined elements, then the corresponding elements of the output array are also undefined. If the input arrays are both ordinal categorical arrays, then so is the output array.

Create Two Categorical Arrays

Create two categorical arrays. To multiply them, either the two arrays must have the same number of elements, or one array must have only one element.

A = categorical(["blue" "red" "green"])
A = 1x3 categorical
     blue      red      green 

B = categorical(["+" "-" "+"])
B = 1x3 categorical
     +      -      + 

However, the two arrays can have different numbers of categories, as shown by categories. The categories function returns the set of categories that a categorical array has.

categories(A)
ans = 3x1 cell
    {'blue' }
    {'green'}
    {'red'  }

categories(B)
ans = 2x1 cell
    {'+'}
    {'-'}

Produce All Combinations of Categories

Multiply the two categorical arrays. The elements of the product come from combinations of the corresponding elements from the input arrays.

C = A.*B
C = 1x3 categorical
     blue +      red -      green + 

However, the categories of the product are all the ordered pairs that can be created from the categories of the two inputs. So, it is possible that some categories are not represented by any elements of the output array.

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

The order of the categories of the product follows from the orders of the categories of the input arrays. Therefore, B.*A does not equal A.*B.

D = B.*A
D = 1x3 categorical
     + blue      - red      + green 

The categories of B.*A are also different from the categories of A.*B.

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

Combinations from Arrays with Undefined Elements

Multiply two categorical arrays. If either A or B have an undefined element, the corresponding element of C is also undefined. You can create undefined elements by using the missing function.

A = categorical(["blue" "red" "green" missing])
A = 1x4 categorical
     blue      red      green      <undefined> 

B = categorical(["+" missing "+" "-"])
B = 1x4 categorical
     +      <undefined>      +      - 

C = A.*B
C = 1x4 categorical
     blue +      <undefined>      green +      <undefined> 

However, the presence of undefined elements in the inputs does not change the categories of the output array. Undefined elements do not belong to a category.

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

Combinations from Ordinal Categorical Arrays

Create two ordinal categorical arrays. Display the categories of each array.

A = categorical(["blue" "red" "green" "red" "green" "red"], ...
                ["green" "red" "blue"], ...
                Ordinal=true)
A = 1x6 categorical
     blue      red      green      red      green      red 

categories(A)
ans = 3x1 cell
    {'green'}
    {'red'  }
    {'blue' }

B = categorical(["+" "-" "+" "-" "+" "-"], ...
                Ordinal=true)
B = 1x6 categorical
     +      -      +      -      +      - 

categories(B)
ans = 2x1 cell
    {'+'}
    {'-'}

Multiply the arrays.

C = A.*B
C = 1x6 categorical
     blue +      red -      green +      red -      green +      red - 

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

The output array C is an ordinal categorical array because A and B are both ordinal.

isordinal(C)
ans = logical
   1

Return All Combinations as Table

Since R2023a

You can also produce all combinations of categories by using the combinations function. When you use combinations, the two input arrays can have different lengths. However, the output is a table, not a categorical array. The table variables are separate categorical arrays. The table does not contain a categorical array whose categories are the combinations shown in the table.

A1 = categorical(["blue" "red" "green" "black"])
A1 = 1x4 categorical
     blue      red      green      black 

A2 = categorical(["+" "-"])
A2 = 1x2 categorical
     +      - 

T = combinations(A1,A2)
T=8×2 table
     A1      A2
    _____    __

    blue     + 
    blue     - 
    red      + 
    red      - 
    green    + 
    green    - 
    black    + 
    black    - 

See Also

| | | | |

Related Topics