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
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
n_or_v | Any | Yes | — | Nonnegative integer scalar n or vector v containing choices. |
k | NumericScalar | Yes | — | Number of choices to select. |
Returns
| Name | Type | Description |
|---|---|---|
C | Any | Binomial coefficient or matrix whose rows contain all selected combinations. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:nchoosek:InvalidInput | Inputs are missing, malformed, unsupported, or not valid nonnegative integer choices. | nchoosek: invalid input |
RunMat:nchoosek:TooLarge | The combinations matrix would exceed RunMat's supported materialisation limit. | nchoosek: output is too large |
RunMat:nchoosek:Internal | Output allocation, GPU gather, or container construction failed. | nchoosek: internal error |
How nchoosek works
- Scalar coefficient mode accepts nonnegative integer numeric scalars.
nandkmust 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
kcolumns and one row per combination. k = 0returns a 1-by-0 empty combinations matrix.- When
kis 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 = 10List 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.
Related Array functions
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
Grouping
accumarray · combinations · discretize · findgroups · groupcounts · grp2idx · splitapply
Sorting Sets
argsort · intersect · ismember · ismembertol · issorted · issortedrows · setdiff · setxor · sort · sortrows · union · unique
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how nchoosek is executed, line by line, in Rust.
- View the source for nchoosek in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
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.