Code covered by the BSD License  

Highlights from
COMBINATOR -combinations AND permutations

5.0

5.0 | 8 ratings Rate this file 41 Downloads (last 30 days) File Size: 32.66 KB File ID: #24325
image thumbnail

COMBINATOR -combinations AND permutations

by Matt Fig

 

02 Jun 2009 (Updated 09 Sep 2010)

Returns 1 of 4 different samplings on the set 1:N, taken K at a time.

| Watch this File

File Information
Description

COMBINATOR will return one of 4 different samplings on the set 1:N, taken K at a time. These samplings are given as follows:
   
PERMUTATIONS WITH REPETITION/REPLACEMENT
  COMBINATOR(N,K,'p','r') -- N >= 1, K >= 0

PERMUTATIONS WITHOUT REPETITION/REPLACEMENT
  COMBINATOR(N,K,'p') -- N >= 1, N >= K >= 0

COMBINATIONS WITH REPETITION/REPLACEMENT
  COMBINATOR(N,K,'c','r') -- N >= 1, K >= 0

COMBINATIONS WITHOUT REPETITION/REPLACEMENT
  COMBINATOR(N,K,'c') -- N >= 1, N >= K >= 0

Example:

combinator(4,2,'p','r') % Permutations with repetition
combinator(4,2,'p') % Permutations without repetition
combinator(4,2,'c','r') % Combinations with repetition
combinator(4,2,'c') % Combinations without repetition
ans =
     1 1
     1 2
     1 3
     1 4
     2 1
     2 2
     2 3
     2 4
     3 1
     3 2
     3 3
     3 4
     4 1
     4 2
     4 3
     4 4
ans =
     1 2
     1 3
     1 4
     2 1
     2 3
     2 4
     3 1
     3 2
     3 4
     4 1
     4 2
     4 3
ans =
     1 1
     1 2
     1 3
     1 4
     2 2
     2 3
     2 4
     3 3
     3 4
     4 4
ans =
     1 2
     1 3
     1 4
     2 3
     2 4
     3 4

The accompanying c++ file can be MEXed to provide the ability to specify N as an int8, int16, or int32. This saves memory and is faster. I have provided a MEX file that was created on Win XP with 2006a that may work. If not, the file will need to be MEXed on your machine.
Please READ the help before using.
I would very much appreciate bug reports sent through email, as well as suggestions for improvement. Thanks.

Acknowledgements
This submission has inspired the following:
VChooseK, VChooseKRO, VChooseKR, VChooseKO, ICHOOSE
MATLAB release MATLAB 7.4 (R2007a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (17)
02 Jun 2009 us

a very nice package/wrapper for a lot of the combinatorial problems almost daily asked for in the NG - or - as the name implies: a TERMINATOR for combiners...

in particular:
- excellent help/good example
- clean engine divided into intelligible subfunctions
- many comments and timings

us

02 Jun 2009 Bruno Luong

Two words: excellent job!

25 Aug 2009 Andrey

Very good.

31 Oct 2009 mklcst mklcst

If I put a value greater than 170 I get an error.

31 Oct 2009 Matt Fig

Michele,
You didn't give very much information. If you put a number greater than 170 WHERE? And what other parameters? Why don't you just email me? I put my email in the help for that purpose. If you email me, show me EXACTLY what you did, don't be vague.

07 Jan 2010 Jan Simon

Efficient, compact, clear, comprehensive help and comments in the source, comparisons with other functions from the FEX.

Small improvement: In perms_loop the term "[i*ones(m,1) t]" is slower then assigning [i] and [t] separately. I simplified the computation of indices (just some % faster). Then the loop becomes:
for n = 2:M
    q = P;
    m = G(n-1);
    P = zeros(n*m, n, CN);
    P(1:m, 1) = n;
    P(1:m, 2:n) = q;
    
    a = m + 1;
    for i = n-1:-1:1,
        t = q;
        t(t == i) = n;
        b = a + m - 1;
        P(a:b, 1) = i;
        P(a:b, 2:n) = t;
        a = b + 1;
    end
end
For Matlab 7.8, this is about 30% faster than the original subroutine perms_loop. To my surprise a method with pre-allocating the complete output at once and avoiding a growing is some percent slower.

Thanks, Jan

08 Jan 2010 Matt Fig

Thanks, Jan. I will look into your suggestions and, if my findings match yours, will offer an update. I appreciate the speedup!

13 Jan 2010 Jan Simon

Another idea: COMBINATOR(200, 2, 'p') fails due to an overflow - without need. You could replace this:
  prod(1:M) / (prod(1:(M-K))) => prod(M-k+1:M)
in the calculation of BC and BC1 in perms_no_rep.
Similar in combs_rep: prod(M:(M+K-1)) / prod(1:K) is more susceptible for overflows than: prod((M:(M+K-1)) ./ (1:K)). Then COMBINATOR can handle large N as long as K is small enough.
I've implemented a lot of algorithms for permutations and combinations, e.g. Knuth's. My impression: COMBINATOR is very fast and memory efficient and I do not expect, that a Matlab implementation can be faster! Even a C-MEX for COMBINATOR(N,K,'p') with comparable speed is a hard work. Unfortunately I've rated this file already - so just: thanks again.

02 Mar 2010 Tal Shir

Just what I needed. Thanks.

22 Jun 2010 almacellesiti Fernandez

Nice one Matt.

There might be a small problem though. With matlab 2009 in windows and matlab 2008 in linux I can reproduce the following problem.

combinator(24,23,'c')

leaves the last row of the return matrix to zeros. It does not update it because the parameter

BC=prod(M-K+1:M) / (prod(1:K));

inside the function function CN = combs_no_rep(N,K), at the line 343 of combinator.m

results in BC=23.999999999999996 (instead of the correct value, 24)

I guess a round in that line will take care of it.
A few other combinations of numbers give no problem.

09 Aug 2010 Jan Simon

Your first submission contained the source code cumsumall.cpp. Now I find only the compiled mexw32. It would be nice to have the source again, to allow all non-Windows and the 64 bitters to work with COMBINATOR.

10 Aug 2010 Matt Fig

Oops, that was an error of omission on my part. I will post an update. Thanks Jan.

23 Sep 2010 Stephan Schmidt

Great package, exactly what I needed for http://www.mathworks.de/matlabcentral/newsreader/view_thread/292210

Thanks a lot Matt!

10 Apr 2011 Chien-Chia Huang

Thanks a lot, Matt.
It does help.

25 May 2011 mortain Antonio

Hey Matt, I came back on your software and I found a changed a value of the original one that's why it was giving the error. It's an amazing software...clear and so elegant!

Thank you very much

29 Aug 2011 Xavier

Great code, very fast and just what I needed... but I'm getting an error on the output data when I run the following case:

COMBINATOR(50,n,'c')

the last line has only zeros when n is 46 or 47, but not when it is 44, 45 or 48.

Can you check what's wrong?

17 Apr 2012 7ate9

Works well for the few tests I ran. Had to compile for 64 bit, but that was painless. Thanks!

Please login to add a comment or rating.
Updates
16 Jun 2009

Added ability to specify integer class for N. MEX-File.

27 Jan 2010

Implemented changes suggested by Jan Simon.

09 Sep 2010

Previous update had left out the C++ source code.

Tag Activity for this File
Tag Applied By Date/Time
combinatorics Matt Fig 02 Jun 2009 09:49:19
permutations Matt Fig 02 Jun 2009 09:49:19
combinations Matt Fig 02 Jun 2009 09:49:19
set Matt Fig 02 Jun 2009 09:49:19
multichoose Matt Fig 02 Jun 2009 09:49:19
nchoosek Matt Fig 02 Jun 2009 09:49:19
perms Matt Fig 02 Jun 2009 09:49:19
combinations Samuel 20 Jan 2011 07:38:29

Contact us at files@mathworks.com