View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from

5.0 | 3 ratings Rate this file 4 Downloads (last 30 days) File Size: 2.1 KB File ID: #23064 Version: 1.0




Obtain a single combination of N numbers taken K at a time.

| Watch this File

File Information

comb = ONECOMB(N,K,M) returns the M-th combination of the sorted list of all combinations from NCHOOSEK.
Makes use of a fast algorithm for the generation of a single combination.

NCHOOSEK(1:4,3) =
[ 1 2 3
  1 2 4
  1 3 4
  2 3 4 ]

onecomb(4,3,3) =
[ 1 3 4 ]


This file inspired Matlab Ppt.

MATLAB release MATLAB 7 (R14)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (4)
04 Mar 2012 Alexey

Alexey (view profile)

Very useful!

What's about reverse function converting conbination to its index?

24 Mar 2010 Husam Aldahiyat

Very useful submission, well written.

16 Jan 2010 Rob Campbell

Rob Campbell (view profile)

This well coded and does what it says. Can be substantially faster than nchoosek. For example:

>> tic, for i=1:1E4;onecomb(10,5,5);end,toc/1E4
ans =
>> tic, for i=1:1E4;NCHOOSEK(1:10,5);end,toc/1E4
ans =

The 1 star review seems to have missed the point.

15 Dec 2009 Darren Rowland

@ Marco/PXlab

It is exactly correct that this function with call structure

C1 = onecomb(N,K,M)

returns the same result as

AC = nchoosek(N,K);
C2 = AC(M,:)

The advantage of this function is that the intermediate array AC is not computed by ONECOMB. The array AC can be immensely large for large N and K approx N/2, and is a waste to compute if one only requires a small number of combinations from the total.

Additionally, if one requires many combinations, my testing (not included here but simple to duplicate) showed that the time to compute all the combinations using NCHOOSEK was approximately the same as computing all the combinations one-at-a-time using ONECOMB.

Comment only

Contact us