Code covered by the BSD License  

Highlights from
COMBN (4.3)

4.82143

4.8 | 28 ratings Rate this file 77 Downloads (last 30 days) File Size: 2.81 KB File ID: #7147

COMBN (4.3)

by

 

15 Mar 2005 (Updated )

All combinations of N elements taken from the vector V.

| Watch this File

File Information
Description

COMBN - all combinations of elements
  M = COMBN(V,N) returns all combinations of N elements of the elements in
  vector V. M has the size (length(V).^N)-by-N.
 
  [M,I] = COMBN(V,N) also returns the index matrix I so that M = V(I).
 
  V can be an array of numbers, cells or strings.
 
  Example:
    M = COMBN([0 1],3) returns the 8-by-3 matrix:
      0 0 0
      0 0 1
      0 1 0
      0 1 1
      ...
      1 1 1
 
  All elements in V are regarded as unique, so M = COMBN([2 2],3) returns
  a 8-by-3 matrix with all elements equal to 2.
 
  NB Matrix sizes increases exponentially at rate (n^N)*N.

  For larger values of n and N, one could loop over the output of COMBNSUB retrieving one or more rows of the output at a single time.
 
  See also PERMS, NCHOOSEK
  and COMBNSUB, ALLCOMB, and PERMPOS here on the FEX

Latest version 4.3 (apr 2013)

Acknowledgements

This file inspired De Bruijn Sequence Generator, Kautz Sequence Generator, V Choose Kro, N Permute K, and Combnsub.

MATLAB release MATLAB 7 (R14)
Other requirements Should work in most ML releases
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (35)
25 Feb 2013 Qi An

saved me sometime!

03 Jan 2013 ted p teng  
04 Jun 2012 Patrizio Manganiello

Thank you very much!

03 Jun 2012 mya

verry useful, thank you

04 Apr 2012 Han

Repetitive combination, great!

25 Mar 2012 Jos (10584)

@Marc, thanks for your interest and suggestions for improvement. Yet, for code compatibility (and fairness) you need to implement to inverse order step as well :-)

23 Mar 2012 Marc Lalancette

Nice code, thanks! Just for fun, slight to medium speed improvement (sorry my k = your N):

%instead of:
%[Y{k:-1:1}] = ndgrid(X) ;
%Y = reshape(cat(k+1,Y{:}),[],k) ;
%fill Y directly:
nX = numel(X);
Y(nX^k, k) = X(1); % Initialize to give shape and data type.
s(1:k-1) = {ones(1, nX)};
for i = 1:k
Y( ((i-1)*nX^k + 1) : (i*nX^k) ) = X(s{1:i-1}, :, s{i:k-1});
X = permute(X, [1:i-1, i+1, i]);
end
% This has inverse order for the combinations as you had, easy to change.

09 Aug 2011 Jos (10584)

@ Talaria, this is impossible as it would require a memory of (at least) 150*(2^150) bits which is about 2700000000000000000000000000000000 Terabyte ...

However, it is unlikely that you need all those combinations at once; perhaps you can think of another approach to your problem, for instance, by drawing a few random combinations at a time?

05 Aug 2011 Talaria

works great, but is there a way to beat the limitation? for example i want to be able to return the following:
M = COMBN([0 1],150)
is this possible?

22 Jul 2011 W. Owen Brimijoin

The core of this function is a lovely little computational gem. Thank you.

29 May 2011 Manu

Excellent and fast!

25 May 2011 Brad Ridder

Excellent, just what I needed. Thanks!

11 Apr 2011 Fabio

Does anyone have an idea on how to create a function like "combn" but where the generated matrix contains only monotone rows? I could do it manually by removing non-monotone rows after having built the matrix, but when the size of the matrix becomes large i got out of memory error. Any idea?

21 Dec 2010 John D'Errico  
21 Jan 2010 Oleg Komarov

Can't find a better suited H1-line. Neat help, simple example, history (maybe more details on the dates...) and past algorithms too.
I don't use combinatorics very much but I always wonder why matlab doesn't cover it esplicitly. A must have.

Oleg

16 Jan 2010 Jan Simon

Nice and comapct program! H1-line, help, history, comments in the code, efficient. Thanks!

But can be made still a little bit faster:
X = repmat({X},1,N) ;
[X{1:N}] = ndgrid(X{:}) ;
X = fliplr(reshape(cat(N+1,X{:}),[],N)) ;
1. If X is filled in reverse order with [X{N:-1:1}], FLIPLR can be omitted => 25% faster for COMBN(1:10, 5) (see my comment for ALLCOMB)
2. Inside NDGRID the same operations are performed for each input, but all inputs are equal. Inlining and simplyfying NDGRID leads to (REPMAT can be omitted then):
C = cell(1, N);
X = X(:);
L = length(X);
s(1:N - 1) = L;
X = reshape(X(:, ones(1, prod(s))), [L, s]);
C{N} = X;
for iC = 2:N
C{N - iC + 1} = permute(X, [2:iC, 1, iC + 1:N]);
end
X = reshape(cat(N+1, C{:}), [], N);
=> 40-50% faster.
To my surprise it is *slower* to create the output array at once and insert the permuted subarrays directly.

14 Dec 2009 Philip L  
23 Mar 2009 Carlos Baiz

Thanks!

06 May 2008 jose caceres

any idea of how to get the combinations without repetition of previous combinations, and without re-using the elements in a a selection?

09 Apr 2008 Riccardo Bevilacqua

Exactly what I was looking for!

15 Jan 2008 Antonio Silva

Thanks a million!

24 Aug 2007 Ohad N

Does the job...

08 May 2007 Jos the author

Roger Stafford pointed out that, due to the IEEE 754 standard, the floor of a floating point (as used in COMBN's algorithm) may lead to faulty results for very specific inputs.
The update incorporates his excellent solution to this potential problem. Thanks Roger!
The update (COMBN version 3.2) should be up soon ...

03 Apr 2007 Kaijun Wang

It is very useful, thanks !

21 Feb 2007 Wolfgang Zieroth

Fast and (almost) exactly what I needed!

... I only need those combinations which sum to (say) one. I can, of course, first create all combinations with this program and then find(m*ones(N,1)==1). Yet I bounce very quickly against maximum variable size for Matlab. Any Suggestions?

16 Jan 2007 ShenKae Wu

Thanks a lot

01 Nov 2006 Vangelis R

I am very surprised this is not a build in function. You saved my day thanks a lot!

25 Jul 2006 B E

Thanks! Exactly what I needed.

11 Jul 2006 Peter Navé

What I needed!

17 Jun 2006 Jürgen Womser-Schütz

Very fast! Very helpful to solve my actual problem.

21 Jan 2006 David Torres

Great tool. Easy to use.

28 Oct 2005 Richard Vance

Easy to use, fast, and suitable for incorporating into other programs. Good documentation well organized. It really helped me solve some problems.

18 Oct 2005 yakir gagnon  
25 May 2005 Jos vdG

Jamie, use COMBN(UNIQUE(V),N)

23 May 2005 Jamie Baier

Does what it says, but there really should be an option to treat elements as non-unique.

Updates
11 May 2006

new faster algorithm

31 Aug 2006

new (very fast) algorithm

08 May 2007

arggg updated with older version ...

18 Jan 2010

modified slightly based on suggestions by Jan Simon (thanks!)

04 Apr 2011

corrected to give column vector output for N=1. (error pointed out by Wilson via email).

09 Apr 2013

Reference to COMBNSUB for large combinatorial problems.

Contact us