Main Content

nchoosek

Binomial coefficient

Description

b = nchoosek(n,k) returns the binomial coefficient of n and k, defined as n!/(k!(n - 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!/(k!(n - k)!) rows, where n is length(v). In this syntax, k must be a nonnegative integer.

example

Examples

Binomial Coefficients for Numeric and Symbolic Arguments

Compute the binomial coefficients for these expressions.

syms n
[nchoosek(n, n), nchoosek(n, n + 1), nchoosek(n, n - 1)]
ans =
[ 1, 0, n]

If one or both parameters are negative numbers, convert these numbers to symbolic objects.

[nchoosek(sym(-1), 3), nchoosek(sym(-7), 2), nchoosek(sym(-5), -5)]
ans =
[ -1, 28, 1]

If one or both parameters are complex numbers, convert these numbers to symbolic objects.

[nchoosek(sym(i), 3), nchoosek(sym(i), i), nchoosek(sym(i), i + 1)]
ans =
[ 1/2 + 1i/6, 1, 0]

Handle Expressions Containing Binomial Coefficients

Many functions, such as diff and expand, can handle expressions containing nchoosek.

Differentiate the binomial coefficient.

syms n k
diff(nchoosek(n, 2))
ans =
-(psi(n - 1) - psi(n + 1))*nchoosek(n, 2)

Expand the binomial coefficient.

expand(nchoosek(n, k))
ans =
-(n*gamma(n))/(k^2*gamma(k)*gamma(n - k) - k*n*gamma(k)*gamma(n - k))

Pascal Triangle

Use nchoosek to build the Pascal triangle.

m = 5;
for n = 0:m
    C = sym([]);
  for k = 0:n
    C = horzcat(C, nchoosek(n, k));
  end
  disp(C)
end
1
[ 1, 1]
[ 1, 2, 1]
[ 1, 3, 3, 1]
[ 1, 4, 6, 4, 1]
[ 1, 5, 10, 10, 5, 1]

All Combinations of Vector Elements

Find all combinations of elements of a 1-by-5 symbolic row vector taken three and four at a time.

Create a 1-by-5 symbolic vector with the elements x1, x2, x3, x4, and x5.

v = sym('x', [1, 5])
v =
[ x1, x2, x3, x4, x5]

Find all combinations of the elements of v taken three at a time.

C = nchoosek(v, 3)
C =
[ x1, x2, x3]
[ x1, x2, x4]
[ x1, x3, x4]
[ x2, x3, x4]
[ x1, x2, x5]
[ x1, x3, x5]
[ x2, x3, x5]
[ x1, x4, x5]
[ x2, x4, x5]
[ x3, x4, x5]
C = nchoosek(v, 4)
C =
[ x1, x2, x3, x4]
[ x1, x2, x3, x5]
[ x1, x2, x4, x5]
[ x1, x3, x4, x5]
[ x2, x3, x4, x5]

Input Arguments

collapse all

Number of possible choices, specified as a symbolic number, variable, expression, or function.

Number of selected choices, specified as a symbolic number, variable, expression, or function. If the first argument is a symbolic vector v, then k must be a nonnegative integer.

Set of all choices, specified as a vector of symbolic numbers, variables, expressions, or functions.

Output Arguments

collapse all

Binomial coefficient, returned as a nonnegative scalar value.

All combinations of v, returned as a matrix of the same type as v.

More About

collapse all

Binomial Coefficient

If n and k are integers and 0 ≤ k ≤ n, the binomial coefficient is defined as:

(nk)=n!k!(nk)!

For complex numbers, the binomial coefficient is defined via the gamma function:

(nk)=Γ(n+1)Γ(k+1)Γ(nk+1)

Tips

  • Calling nchoosek for numbers that are not symbolic objects invokes the MATLAB® nchoosek function.

  • If one or both parameters are complex or negative numbers, convert these numbers to symbolic objects using sym, and then call nchoosek for those symbolic objects.

Algorithms

If k < 0 or n – k < 0, nchoosek(n,k) returns 0.

If one or both arguments are complex, nchoosek uses the formula representing the binomial coefficient via the gamma function.

Version History

Introduced in R2012a

See Also

| | |