(Not recommended) 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.
combnkis not recommended
Not recommended starting in R2020b
combnk is not recommended. Use the MATLAB function
nchoosek instead. There are no plans to remove
To update your code, change instances of the function name
nchoosek. You do not need to change the input arguments. For
C = nchoosek(v,k). The output
all possible combinations of the elements of vector
k at a time. Note that
nchoosek can have a different order compared to the output from
nchoosek function has several advantages over the
nchoosek also returns the binomial coefficient when the first
input argument is a scalar value.
nchoosek has extended functionality using MATLAB
nchoosek is faster than