Quantcast

Documentation Center

  • Trial Software
  • Product Updates

nchoosek

Binomial coefficient or all combinations

Syntax

Description

example

b = nchoosek(n,k) returns the binomial coefficient, defined as n!/((nk)! k!). This is the number of combinations of n items taken k at a time.

example

C = nchoosek(v,k) returns a matrix containing all possible combinations of the elements of vector v taken k at a time. Matrix C has k columns and n!/((nk)! k!) rows, where n is length(v).

Examples

expand all

All Combinations of Five Numbers Taken Four at a Time

v = 2:2:10;
C = nchoosek(v,4)
C =

     2     4     6     8
     2     4     6    10
     2     4     8    10
     2     6     8    10
     4     6     8    10

All Combinations of Three Unsigned Integers Taken Two at a Time

v = uint16([10 20 30]);
C = nchoosek(v,uint16(2))
C =

     10     20
     10     30
     20     30

Input Arguments

expand all

n — Number of possible choicesscalar, real, nonnegative value

Number of possible choices, specified as a scalar value of any numeric type that is real and nonnegative.

Example: 10

Example: int16(10)

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

k — Number of selected choicesscalar, real, nonnegative value

Number of selected choices, specified as a scalar value that is real and nonnegative. k can be any numeric type. However, nchoosek(n,k) requires that n and k be the same type or that at least one of them be of type double.

There are no restrictions on combining inputs of different types for nchoosek(v,k).

Example: 3

Example: int16(3)

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

v — Set of all choicesvector of numeric, logical, or char values

Set of all choices, specified as a vector of numeric, logical, or char values.

Example: [1 2 3 4 5]

Example: [1+1i 2+1i 3+1i 4+1i]

Example: int16([1 2 3 4 5])

Example: [true false true false]

Example: ['abcd']

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char
Complex Number Support: Yes

Output Arguments

expand all

b — Binomial coefficientnonnegative scalar value

Binomial coefficient, returned as a nonnegative scalar value. b is the same type as n and k. If n and k are of different types, then b is returned as the nondouble type.

C — All combinations of vmatrix

All combinations of v, returned as a matrix of the same type as v. Matrix C has k columns and n!/((nk)! k!) rows, where n is length(v).

Each row of C contains a combination of k items chosen from v. The elements in each row of C are listed in the same order as they appear in v.

Limitations

  • When b = nchoosek(n,k) is sufficiently large, nchoosek displays a warning that the result might not be exact. In this case, the result is only accurate to 15 digits for double-precision inputs, or 8 digits for single-precision inputs.

  • C = nchoosek(v,k) is only practical for situations where length(v) is less than about 15.

See Also

Was this topic helpful?