You can use the combnk
function to find all combinations of n
elements. It takes two arguments: the set of elements and the number of elements to choose in each combination. Here's an example:
main.m91 chars5 lines
The above code will compute all combinations of two elements from the set 1:4
.
The resulting combinations
matrix will be an nCr x num_elements
matrix where nCr
is the number of combinations of n
elements taken r
at a time (i.e., n!/((n-r)!r!)
).
For the example above, the combinations
matrix will be:
main.m36 chars7 lines
Each row represents a unique combination of two elements from the set 1:4
.
gistlibby LogSnag