Code covered by the BSD License  

Highlights from
ALLCOMB

5.0

5.0 | 16 ratings Rate this file 167 Downloads (last 30 days) File Size: 2.2 KB File ID: #10064

ALLCOMB

by Jos (10584)

 

20 Feb 2006 (Updated 13 Jan 2012)

All combinations of input (v2.2, jan 2012)

Editor's Notes:

This file was a File Exchange Pick of the Week

| Watch this File

File Information
Description

B = ALLCOMB(A1,A2,A3,...,AN) returns all combinations of the elements in A1, A2, ..., and AN. B is P-by-N matrix is which P is the product of the number of elements of the N inputs.

Empty inputs yields an empty matrix B of size 0-by-N. Note that previous versions (1.x) simply ignored empty inputs.

Example:
allcomb([1 3 5],[-3 8],[0 1]) ;
        1 -3 0
        1 -3 1
        1 8 0
        ...
        5 -3 1
        5 8 0
        5 8 1
     
ALLCOMB(A1,..AN,'matlab') causes the first column to change fastest. This is more consistent with matlab indexing. Example:
allcomb(1:2,3:4,5:6,'matlab') %->
       1 3 5
       2 3 5
       1 4 5
       ...
       2 4 6
 
This functionality is also known as the cartesian product.

See also <COMBN> here on the FEX

MATLAB release MATLAB 6.5 (R13)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (27)
21 Feb 2006 John D'Errico

Good help, error checks, not much to say here. Only one comment: I'd argue that the first argument should move most rapidly, to be consistent with Matlab indexing.

22 Feb 2006 Jos van der Geest

I just updated this file: a minor "bug" was removed when none of the inputs were empty. I also added the option to let the first argument move most rapidly, as suggested by John D'Errico.

03 Nov 2006 Antoni J. CanĂ³s

Excellent! That's what I needed!

06 Jun 2007 Jarek Zwolski

I'd lost some time before I found it. This short piece of code sloves my problem and does it excellent. Thanks

20 Feb 2008 kinor Mueller

nice function, poor not to find it in matlab itself ;-)

31 Mar 2008 Jiying Yin

where can I find allcomb.m file? Thanks!

29 May 2008 Tom Pinkiewicz

Great, just what I wanted!

06 Aug 2008 Marco Vilela

Good job

19 Sep 2008 Merrill Birdno

Thanks! I've been looking for something like this.

14 Nov 2008 fabio

easy things are always the best! Great!

19 Jun 2009 Tsinghua Jun

so powerful, it is what I've been looking for~~

06 Aug 2009 Andrea Asoni

very useful. it is possible to have the exact same thing for symbolic objects (i am working with tomSym)?

30 Dec 2009 Jan Simon

Comapct and fast algorithm - thanks!
The flipping can be omitted to save time:
  [A{1:ni}] = ndgrid(args{ii}) ;
  A = reshape(cat(ni+1,A{:}),[],ni) ;
  A = A(:,ii) ;
==>
  [A{ii}] = ndgrid(args{ii}) ;
  A = reshape(cat(ni+1,A{:}),[],ni) ;
E.g. for ALLCOMB(1:100, 1:100, 1:100) this is 24% faster on my machine (Matlab 2009a, WinXP, 32 bit).

09 Jul 2010 Peter

Very nice!

23 Sep 2010 Jose  
02 Dec 2010 Lin Li  
24 Dec 2010 Bruno Luong

Jos, IMO allcomb returns incorrect result when one of the argument is is empty

It should return an empty (0xn) array where n is the number of arguments.

24 Dec 2010 Jos (10584)

Hmm, there already is a warning ... I should read my own code more carefully ...

24 Dec 2010 Jos (10584)

Somehow my first reply to Bruno does not show up. I wanted to say to Bruno "maybe I can issue a warning when an input is empty".
If ALLCOMB returns empty or ignores an empty input is, in my opinion, a matter of debate.

24 Dec 2010 Bruno Luong

Here is a debate that will convince you Jos.

A cartesian's product of n sets has a cardinal equals to the product of cardinals of each set.

NCOMB must return [prod(numel(xi)) x n] size array.

An empty set has zeros cardinal, thus all comb MUST return 0 x n.

This is a correct answer.

Why this is important. Here is an example

In post #4 of this thread
http://www.mathworks.com/matlabcentral/newsreader/view_thread/299532

I used NDGRID to generate a combination of 2 sets. NDGRID handles correctly empty configuration, this my code can directly work. I can't replace with ALLCOMB because the empty set is not consistently handle.

29 Dec 2010 Bruno Luong  
03 Jan 2011 Marco Uzielli

Very efficient in my case.

11 Feb 2011 Mike Sheppard

In the Neural Network Toolbox there is combvec, which produces the same results as the examples.
combvec([1 3 5],[-3 8],[0 1])
combvec(1:2,3:4,5:6)
If you have the toolbox the combvec might be helpful, if not then allcomb might help for general.

30 May 2011 Forrest

Can you explain how to install this and get it to work?

23 Oct 2011 Harry MacDowel

Great and easy to use!

11 Jan 2012 Edward Sciberras

Jos, I think you're missing a semicolon in line 60 for ischar(argn). Results in 0 being printed in the terminal without this.

13 Jan 2012 Jos (10584)

Thanks Edward, a small glitch on my side ... fixed in version 2.2

Please login to add a comment or rating.
Updates
23 Feb 2006

Too hasty in my first revision ...

23 Feb 2006

1) Removed a erroneous warning when no empty inputs were _not_ provided.
2) Added the option to let the first input run fastest (thanks to John D'Errico)

04 Jan 2010

little speed improvement as suggested by Jan Simon

26 Dec 2010

Convinced by Bruno, I decided to change the behavior when an input is empty (now returns empty instead of ignoring the input).

11 Feb 2011

(2.1, feb 2011) - fixed the check of the last argument when the last argument was neither a char or a double.

13 Jan 2012

remove superfluous line of code (thanks to Edward)

Tag Activity for this File
Tag Applied By Date/Time
matrices Jos (10584) 22 Oct 2008 08:16:17
all Jos (10584) 23 Oct 2008 06:29:54
combination Jos (10584) 23 Oct 2008 06:29:54
permutation Jos (10584) 23 Oct 2008 06:29:54
cartesian product Jos (10584) 23 Oct 2008 06:29:54
select Jos (10584) 23 Oct 2008 06:29:54
repetion Tsinghua Jun 19 Jun 2009 21:44:57
combinations Jos (10584) 31 May 2010 03:33:19
repetition Jos (10584) 31 May 2010 03:33:46
permutations Jos (10584) 31 May 2010 03:33:55

Contact us at files@mathworks.com