Code covered by the BSD License  

Highlights from
ICHOOSE

5.0

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

ICHOOSE

by Jonas Lundgren

 

24 Feb 2011 (Updated 21 Nov 2011)

Choose K indices from the set 1:N.

| Watch this File

File Information
Description

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

EXAMPLE:
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)

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
COMBINATOR -combinations AND permutations, VChooseK

MATLAB release MATLAB 7.10 (2010a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (2)
18 Mar 2011 Jan Simon

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

18 Mar 2011 Jonas Lundgren

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.

Please login to add a comment or rating.
Updates
21 Nov 2011

New contact info

Tag Activity for this File
Tag Applied By Date/Time
choose Jonas Lundgren 24 Feb 2011 09:25:24
mathematics Jonas Lundgren 24 Feb 2011 09:25:25
combinations Jonas Lundgren 24 Feb 2011 09:25:25
nchoosek Jonas Lundgren 24 Feb 2011 09:25:25

Contact us at files@mathworks.com