Code covered by the BSD License  

Highlights from

5.0 | 1 rating Rate this file 10 Downloads (last 30 days) File Size: 1.51 KB File ID: #30523 Version: 1.1



Jonas Lundgren (view profile)


24 Feb 2011 (Updated )

Choose K indices from the set 1:N.

| Watch this File

File Information

ICHOOSE(N,K) gives all combinations of choosing K indices
from the set 1:N without order and without repetitions.

ind = ichoose(4,2);
v = 'ABCD';
v(ind) % ['AB';'AC';'AD';'BC';'BD';'CD']

ICHOOSE is certainly not unique. Here is a comparison with other codes
using TIMEIT by Steve Eddins. Note that VCHOOSEK is a MEX-code.

f1 = @() nchoosek(1:24,8);
f2 = @() combinator(24,8,'c');
f3 = @() ichoose(24,8);
f4 = @() VChooseK(int8(1:24),8);

timeit(f1) % 16.1 sec (NCHOOSEK by MathWorks)
timeit(f2) % 1.17 sec (COMBINATOR by Matt Fig)
timeit(f3) % 0.110 sec (ICHOOSE by Jonas Lundgren)
timeit(f4) % 0.066 sec (VCHOOSEK by Jan Simon)


Combinator Combinations And Permutations and V Choose K inspired this file.

MATLAB release MATLAB 7.10 (R2010a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (2)
18 Mar 2011 Jonas Lundgren

Jonas Lundgren (view profile)

Jan, thank you for the feedback. The compiler is the one that comes with Matlab 2010a. Sorry if that is a slow one. I rarely use MEX-code. I am fully satisfied if ichoose is five times slower than VChooseK.

Comment only
18 Mar 2011 Jan Simon

Jan Simon (view profile)

A very nice implementation. The speed difference to Matlab's NCHOOSEK is dramatic.
H1 line, short but descriptive help text, author and date mentioned, check of inputs => 5 stars.

How did you compile VChooseK.c? If I use MSVC2008 the Mex I get these timings (Matlab 2009a, 1.5GHz Pentium-M, WinXP 32):
tic; q=ichoose(24, 8);toc
=> 0.255sec
tic; q=VChooseK(uint8(1:):uint8(24), 8); toc
=> 0.049sec

21 Nov 2011 1.1

New contact info

Contact us