nchoosek — Return a binomial coefficient or all k-combinations of a vector.

nchoosek(n, k) returns the binomial coefficient for selecting k items from scalar n. nchoosek(v, k) returns a matrix whose rows contain every k-element combination from vector v in input order.

Syntax

b = nchoosek(n, k)
C = nchoosek(v, k)

Inputs

NameTypeRequiredDefaultDescription
n_or_vAnyYesNonnegative integer scalar n or vector v containing choices.
kNumericScalarYesNumber of choices to select.

Returns

NameTypeDescription
CAnyBinomial coefficient or matrix whose rows contain all selected combinations.

Errors

IdentifierWhenMessage
RunMat:nchoosek:InvalidInputInputs are missing, malformed, unsupported, or not valid nonnegative integer choices.nchoosek: invalid input
RunMat:nchoosek:TooLargeThe combinations matrix would exceed RunMat's supported materialisation limit.nchoosek: output is too large
RunMat:nchoosek:InternalOutput allocation, GPU gather, or container construction failed.nchoosek: internal error

How nchoosek works

  • Scalar coefficient mode accepts nonnegative integer numeric scalars. n and k must have the same type unless one input is double, and mixed double/nondouble inputs return the nondouble class when the result fits.
  • Vector mode accepts numeric, complex, logical, and character vectors, including row vectors, column vectors, and empty vectors.
  • For vector inputs, the output has k columns and one row per combination.
  • k = 0 returns a 1-by-0 empty combinations matrix.
  • When k is greater than the vector length, vector mode returns a 0-by-k empty matrix.
  • Unsupported containers such as string arrays, cells, structs, objects, and sparse arrays raise an input error.

Does RunMat run nchoosek on the GPU?

GPU inputs are gathered; no GPU kernels are launched.

GPU memory and residency

The result is a host value.

Examples

Compute a binomial coefficient

b = nchoosek(5, 2)

Expected output:

b = 10

List combinations from a numeric vector

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

Expected output:

C = [10 20; 10 30; 20 30]

List character combinations

C = nchoosek('abc', 2)

Expected output:

C = ['ab'; 'ac'; 'bc']

Using nchoosek with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how nchoosek changes the result.

Run a small nchoosek example, explain the result, then change one input and compare the output.

FAQ

Does nchoosek run on the GPU?

No GPU kernel is launched. RunMat gathers gpuArray inputs and materializes the scalar or combinations matrix on the host.

Why can nchoosek(v, k) fail for large vectors?

The number of output rows grows combinatorially. RunMat rejects outputs that exceed its materialization limit rather than attempting an impractically large allocation.

Creation

colon · eye · false · fill · full · inf · linspace · logspace · magic · meshgrid · nan · ndgrid · nonzeros · ones · peaks · perms · rand · randi · randn · randperm · range · sparse · spdiags · speye · spones · sprand · true · zeros

Shape

blkdiag · cat · circshift · diag · flip · fliplr · flipud · horzcat · ipermute · kron · permute · repelem · repmat · reshape · rot90 · squeeze · toeplitz · tril · triu · vertcat

Indexing

find · ind2sub · sub2ind

Introspection

iscolumn · isempty · ismatrix · isrow · isscalar · isvector · length · ndims · numel · size

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how nchoosek is executed, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.

  • RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
  • Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.