Code covered by the BSD License

### Highlights from ALLCOMB

5.0
5.0 | 28 ratings Rate this file 183 Downloads (last 30 days) File Size: 4.21 KB File ID: #10064 Version: 1.6

# ALLCOMB

### Jos (10584) (view profile)

20 Feb 2006 (Updated )

All combinations of input (v4.0, feb 2014)

### Editor's Notes:

This file was a File Exchange Pick of the Week

File Information
Description

ALLCOMB - All combinations
B = ALLCOMB(A1,A2,A3,...,AN) returns all combinations of the elements
in the arrays 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. This functionality is also
known as the Cartesian Product. The arguments can be numerical and/or
characters, or they can be cell arrays.

Examples:
allcomb([1 3 5],[-3 8],[0 1]) % numerical input:
% -> [ 1 -3 0
% 1 -3 1
% 1 8 0
% ...
% 5 -3 1
% 5 8 1 ] ; % a 12-by-3 array

allcomb('abc','XY') % character arrays
% -> [ aX ; aY ; bX ; bY ; cX ; cY] % a 6-by-2 character array

allcomb('xy',[65 66]) % a combination
% -> ['xA' ; 'xB' ; 'yA' ; 'yB'] % a 4-by-2 character array

allcomb({'hello','Bye'},{'Joe', 10:12},{99999 []}) % all cell arrays
% -> { 'hello' 'Joe' [99999]
% 'hello' 'Joe' []
% 'hello' [1x3 double] [99999]
% 'hello' [1x3 double] []
% 'Bye' 'Joe' [99999]
% 'Bye' 'Joe' []
% 'Bye' [1x3 double] [99999]
% 'Bye' [1x3 double] [] } ; % a 8-by-3 cell array

ALLCOMB(..., 'matlab') causes the first column to change fastest which
is consistent with matlab indexing. Example:
allcomb(1:2,3:4,5:6,'matlab')
% -> [ 1 3 5 ; 1 4 5 ; 1 3 6 ; ... ; 2 4 6 ]

If one of the arguments is empty, ALLCOMB returns a 0-by-N empty array.

and nchoose, combn, kthcombn (matlab Central FEX)

Acknowledgements

This file inspired Create Unique Interaction Variables.

MATLAB release MATLAB 7.13 (R2011b)
26 May 2015 Kenan Turbic

### Kenan Turbic (view profile)

04 Apr 2015 Sergei Paleichuk

### Sergei Paleichuk (view profile)

04 Jul 2014 Wasiur Rahman Khuda Bukhsh

### Wasiur Rahman Khuda Bukhsh (view profile)

21 Mar 2014 Jos (10584)

### Jos (10584) (view profile)

@Alexander Implementing this would require the Statistics Toolbox. However, you can easily generate the table yourself, using ALLCOMB on the indices. I assume you should be able to do something like this:

idxAB = allcomb(1:numel(A),1:numel(B))
out = [A(idxAB(:,1)) B(idxAB(:,2))]

Comment only
19 Mar 2014 Alexander

### Alexander (view profile)

Excellent function! Given that this function can be used to create combinations of selections from a number of categories (A1...AN), could you allow it to operate on categorical arrays? The output could be either a table (preferably) or cell array, as in the example below.

A = categorical({'A1','A2'})
B = categorical({'B1','B2','B3'})
allcomb2(A,B)

% -> Var1 Var2
% ____ ____
%
% A1 B1
% A1 B2
% A1 B3
% A2 B1
% A2 B2
% A2 B3

11 Mar 2014 Giorgio

### Giorgio (view profile)

13 Sep 2013 shiva

### shiva (view profile)

This is an awesome piece of code!
Although i have an request, like say when I have a 5 or more large vectors (eg.,-200:200) i encounter memory problems.
I was thinking like,
1) check the maximum variable size for the particular computer.
2) if the size of the generated variable is more than the maximum size permitted store the intermediate result in a "mat" file. clear the contents of the variable and append the successive results to the end of the "mat" file

22 Jun 2013 Julian Gomez

### Julian Gomez (view profile)

04 May 2013 Matthieu

### Matthieu (view profile)

Hello,

I wanted to have the index of the computed combinaison.

I propose this solution if other people id interested:

[A index] = allcomb(varargin)
...
...

if nargout>1
create_index = @(x) cellfun(@(y) 1:numel(y), x, 'UniformOutput', false);
idx = create_index(args);
[index{ii}] = ndgrid(idx{ii});
index = reshape(cat(ni+1,index{:}),[],ni) ;
end

19 Apr 2013 raj singh

### raj singh (view profile)

superb, i was trying for this problem, but this Ans solved my problem within second

22 Mar 2013 Jos (10584)

### Jos (10584) (view profile)

@Ofer To combine strings with doubles you can use a cell array. You can use allcomb to retrieve the indices, as in

D = [10 11] % doubles
S = {'s1','s2','s3'}
ix = allcomb(1:numel(D), 1:numel(S))

Comment only
21 Mar 2013 Ofer Rozen

### Ofer Rozen (view profile)

great function. how can i combine strings with doubles?

19 Jan 2013 Moshe Lindner

### Moshe Lindner (view profile)

23 May 2012 Jos (10584)

### Jos (10584) (view profile)

@Amri, good suggestion! I will update the file shortly.

Comment only
23 May 2012 Amri

### Amri (view profile)

it works only with double variables !?
i need the same function but with caracters ?

Comment only
05 Apr 2012 K E

### K E (view profile)

Why not built in to Matlab?

13 Jan 2012 Jos (10584)

### Jos (10584) (view profile)

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

Comment only
11 Jan 2012 Edward Sciberras

### Edward Sciberras (view profile)

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

Comment only
23 Oct 2011 Harry MacDowel

### Harry MacDowel (view profile)

Great and easy to use!

30 May 2011 Forrest

### Forrest (view profile)

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

Comment only
11 Feb 2011 Mike Sheppard

### Mike Sheppard (view profile)

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.

Comment only
03 Jan 2011 Marco Uzielli

### Marco Uzielli (view profile)

Very efficient in my case.

29 Dec 2010 Bruno Luong

### Bruno Luong (view profile)

24 Dec 2010 Bruno Luong

### Bruno Luong (view profile)

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.

Why this is important. Here is an example

In post #4 of this thread

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.

Comment only
24 Dec 2010 Jos (10584)

### Jos (10584) (view profile)

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.

Comment only
24 Dec 2010 Jos (10584)

### Jos (10584) (view profile)

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

Comment only
24 Dec 2010 Bruno Luong

### Bruno Luong (view profile)

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.

Comment only
02 Dec 2010 Lin Li

23 Sep 2010 Jose

### Jose (view profile)

09 Jul 2010 Peter Torrione

### Peter Torrione (view profile)

Very nice!

30 Dec 2009 Jan Simon

### Jan Simon (view profile)

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).

06 Aug 2009 Andrea Asoni

### Andrea Asoni (view profile)

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

19 Jun 2009 Tsinghua Jun

### Tsinghua Jun (view profile)

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

Comment only
14 Nov 2008 fabio

### fabio (view profile)

easy things are always the best! Great!

19 Sep 2008 Merrill Birdno

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

06 Aug 2008 Marco Vilela

Good job

29 May 2008 Tom Pinkiewicz

Great, just what I wanted!

31 Mar 2008 Jiying Yin

where can I find allcomb.m file? Thanks!

Comment only
20 Feb 2008 kinor Mueller

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

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

03 Nov 2006 Antoni J. CanĂ³s

Excellent! That's what I needed!

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.

Comment only
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.

23 Feb 2006

Too hasty in my first revision ...

04 Jan 2010 1.1

little speed improvement as suggested by Jan Simon

26 Dec 2010 1.2

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 1.3

(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 1.4

remove superfluous line of code (thanks to Edward)

23 May 2012 1.5

now accepts character arrays

18 Feb 2014 1.6

v4.0 accepts cell arrays as input