Binomial coefficient or all combinations (2024)

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

Cnk=(nk)=n!(nk)!k!.

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!/((mk)! k!) rows, where m is length(v).

Examples

collapse all

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

nNumber 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

kNumber 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

vSet 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!/((nk)! 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 where length(v) is less than about 15.

Extended Capabilities

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.

Binomial coefficient or all combinations (1)

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

Contact your local office

Binomial coefficient or all combinations (2024)
Top Articles
Latest Posts
Article information

Author: The Hon. Margery Christiansen

Last Updated:

Views: 6227

Rating: 5 / 5 (50 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: The Hon. Margery Christiansen

Birthday: 2000-07-07

Address: 5050 Breitenberg Knoll, New Robert, MI 45409

Phone: +2556892639372

Job: Investor Mining Engineer

Hobby: Sketching, Cosplaying, Glassblowing, Genealogy, Crocheting, Archery, Skateboarding

Introduction: My name is The Hon. Margery Christiansen, I am a bright, adorable, precious, inexpensive, gorgeous, comfortable, happy person who loves writing and wants to share my knowledge and understanding with you.