Enumeration of combinations
Create a character array of every four-letter combination of the characters in the word
C = combnk('tendril',4);
C is a 35-by-4 character array.
Display the last five combinations in the list.
last5 = C(31:35,:)
last5 = 5x4 char array 'tedr' 'tenl' 'teni' 'tenr' 'tend'
List all two-number combinations of the numbers one through four.
C = combnk(1:4,2)
C = 6×2 3 4 2 4 2 3 1 4 1 3 1 2
1:4 is a vector of doubles,
C is a matrix of doubles.
v— Set of all elements
Set of all elements, specified as a vector.
[1 2 3 4 5]
k— Number of selected choices
Number of elements to select, specified as a nonnegative integer scalar.
k can be any numeric type, but must be real.
There are no restrictions on combining inputs of different types for
C— All combinations
Each row of
C contains a combination of
items selected from
v. The elements in each row of
C are listed in the same order as they appear in
k is larger than
C is an empty matrix.
combnk is practical only for situations where
v has fewer than 15 observations.
MATLAB® contains the function
nchoosek, which can also return all combinations of an element vector and has
extended functionality using MATLAB
nchoosek is faster than