combinations
Description
generates all element combinations of input arrays T
= combinations(A1,...,An
)A1,...,An
, where each
row of the output table T
is a combination. These combinations are the
same as the Cartesian product of n
sets of elements where the
n
sets are represented by the input arrays
A1,...,An
.
The number of rows in
T
is the product of the number of elements in each input array. The number of rows is the same as the number of combinations.The number of variables in
T
is the same as the number of input arrays.The data type of each variable in
T
is the same as the data type of the corresponding input array.
Examples
Input Arguments
Tips
Categorical arrays
If your input arrays are categorical arrays, you might want to produce another categorical array whose categories are all combinations of the inputs. Because the
combinations
function returns all combinations in a table, consider using the.*
operator instead.For example, create two categorical arrays and call the
.*
operator.colors = categorical(["blue" "red" "green" "black"]); signs = categorical(["+" "-" "-" "+"]); C = colors .* signs
C = 1×4 categorical array blue + red - green - black +
The result is a categorical array whose categories are all combinations of the categories of the inputs. List the categories using the
categories
function.categories(C)
ans = 8×1 cell array {'black +'} {'black -'} {'blue +' } {'blue -' } {'green +'} {'green -'} {'red +' } {'red -' }
Version History
Introduced in R2023a