Binomial coefficient or all combinations
collapse all in page
Syntax
b = nchoosek(n,k)
C = nchoosek(v,k)
Description
example
b = nchoosek(n,k)
returns the binomial coefficient, defined as
This is the number of combinations of n
items taken k
at a time. n
and k
must be nonnegative integers.
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 m!/((m–k)! k!) rows, where m is length(v)
.
Examples
collapse all
Binomial Coefficient, "5 Choose 4"
Open Live Script
b = nchoosek(5,4)
All Combinations of Five Numbers Taken Four at a Time
Open Live Script
v = 2:2:10;C = nchoosek(v,4)
C = 5×4 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
Open Live Script
v = uint16([10 20 30]);C = nchoosek(v,uint16(2))
C = 3x2 uint16 matrix 10 20 10 30 20 30
Input Arguments
collapse all
n
— Number of possible choices
nonnegative integer scalar
Number of possible choices, specified as a nonnegative integer scalar. n
can be any numeric type, but must be real.
Example: 10
Example: int16(10)
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
k
— Number of selected choices
nonnegative integer scalar
Number of selected choices, specified as a nonnegative integer scalar. k
can be any numeric type, but must be real. 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 typesfor nchoosek(v,k)
.
Example: 3
Example: int16(3)
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
v
— Set of all choices
vector
Set of all choices, specified as a vector.
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
collapse all
b
— Binomial coefficient
nonnegative scalar value
Binomial coefficient, returned as a nonnegative scalar value. b
isthe same type as n
and k
. If n
and k
areof different types, then b
is returned as the nondoubletype.
C
— All combinations of v
matrix
All combinations of v
, returned as a matrixof the same type as v
. Matrix C
has k
columnsand n!/((n–k)! k!) rows, where n is length(v)
.
Each row of C
contains a combination of k
itemschosen from v
. The elements in each row of C
arelisted in the same order as they appear in v
.
If k > numel(v)
, then C
is an empty matrix.
Limitations
When
b = nchoosek(n,k)
is sufficientlylarge,nchoosek
displays a warning that the resultmight not be exact. In this case, the result is only accurate to 15digits for double-precision inputs, or 8 digits for single-precisioninputs.C = nchoosek(v,k)
is only practicalfor situations wherelength(v)
is less than about15
.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
When the first input,
x
, is a scalar,nchoosek
returns a binomial coefficient. In this case,x
must be a nonnegative integer.When the first input,
x
, is a vector,nchoosek
treats it as a set.For large single precision outputs, result may not match exactly with MATLAB®.
See Variable-Sizing Restrictions for Code Generation of Toolbox Functions (MATLAB Coder).
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Version History
Introduced before R2006a
See Also
perms | combinations
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本 (日本語)
- 한국 (한국어)
Contact your local office