How to create all possible combinations in MATLAB

5 views (last 30 days)
Hi everyone,
I have a vector of 8 columns, x=[a b c d e f g h];
where all letters are integer vectors as follows
a=1:1:20;b=1:1:6; c=1:1:15; d=1:1:26; e=1:1:56; f=1:1:56; g=1:1:11; and h=1:1:31 ;
I would like to make a matrix which includes all possible combinations of these letters and have this size
columns = 6
rows= 20*6*15*26*56*56*11*31;
all possible combinations are needed in my situation.
for example; the first combination should look like this : [1 1 1 1 1 1 1 1]
and the last combination should look like this : [20 6 15 26 56 56 11 31]
and the final matrix should look like this ComMat= [1 1 1 1 1 1 1 1; ...... ; 20 6 15 26 56 56 11 31];
I appreciate your support!

Accepted Answer

Stephen23
Stephen23 on 27 Apr 2018
Edited: Stephen23 on 27 Apr 2018
Download Jos's excellent allcomb, and simply use it like this:
allcomb(a,b,c,...)
I leave it up to you to decide if this is possible with your computer memory, or even worth computing at all. Here is a smaller example of its output using the first three of your input vectors:
>> allcomb(a,b,c)
ans =
1 1 1
1 1 2
1 1 3
1 1 4
1 1 5
1 1 6
1 1 7
1 1 8
1 1 9
1 1 10
1 1 11
1 1 12
1 1 13
1 1 14
1 1 15
1 2 1
1 2 2
1 2 3
1 2 4
1 2 5
1 2 6
1 2 7
1 2 8
... lots of lines
20 5 9
20 5 10
20 5 11
20 5 12
20 5 13
20 5 14
20 5 15
20 6 1
20 6 2
20 6 3
20 6 4
20 6 5
20 6 6
20 6 7
20 6 8
20 6 9
20 6 10
20 6 11
20 6 12
20 6 13
20 6 14
20 6 15
>>

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!