combinations

Table of all combinations of the elements of several lists.

Category: Language Fundamentals > Matrices and Arrays

Syntax

T = combinations(A, B, ...)

Returns one row for each way to pick one element from each input.

Description

This is the Cartesian product of the inputs. The last input varies fastest, so the rows read like an odometer. The result is handy for parameter sweeps: loop over the rows of T and run each configuration.

The number of rows grows as the product of the lengths, so a few long inputs can produce a very large table.

Input arguments

A, B, ...
Vectors, string arrays or cell arrays of any length. Inputs of different types are fine.

Output arguments

T
A table with one variable per input and numel(A)*numel(B)*... rows.

Example

Pairs of values

T = combinations([1 2], [3 4])
T2 = combinations([1 2], {'a','b'});
height(T2)
Output
T = 4×2 table

    Var1    Var2
    ____    ____

     1       3  
     1       4  
     2       3  
     2       4  
ans = 4

See also

table nchoosek meshgrid ndgrid perms