Binomial coefficient
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]
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))
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]
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]
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.