Navigation

Operators and Keywords

Function List:

C++ API

: c = nchoosek (n, k)
: c = nchoosek (set, k)

Compute the binomial coefficient of n or list all possible combinations of a set of items.

If n is a scalar then calculate the binomial coefficient of n and k which is defined as

 /   \
 |n |   n (n-1) (n-2) … (n-k+1)       n!
 |  | = ------------------------- =  ---------
 |k |              k!                k! (n-k)!
 \   /

This is the number of combinations of n items taken in groups of size k.

If the first argument is a vector, set, then generate all combinations of the elements of set, taken k at a time, with one row per combination. The result c has k columns and nchoosek (length (set), k) rows.

For example:

How many ways can three items be grouped into pairs?

nchoosek (3, 2)
   ⇒ 3

What are the possible pairs?

nchoosek (1:3, 2)
   ⇒  1   2
       1   3
       2   3

Programming Note: When calculating the binomial coefficient nchoosek works only for non-negative, integer arguments. Use bincoeff for non-integer and negative scalar arguments, or for computing many binomial coefficients at once with vector inputs for n or k.

See also: bincoeff, perms.

Package: octave