Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to write all possible combinations

Subject: How to write all possible combinations

From: Allamarein

Date: 31 Aug, 2009 11:11:07

Message: 1 of 14

I have 14 events.
Any event can value 1, 2 o 3.
Therefore I can write 4782969 (3^14) different combinations in Matlab
language.

I would run a program that give a 4782969x14 matrix with every
possible combinations.
The easiest method (and the most wearisome) is writing 14 'for' cycle
from1 to 3.
I prefer a svelter solution

Subject: How to write all possible combinations

From: Matt Fig

Date: 31 Aug, 2009 11:56:00

Message: 2 of 14

See:

http://www.mathworks.com/matlabcentral/fileexchange/24325
or
http://www.mathworks.com/matlabcentral/fileexchange/11462

Subject: How to write all possible combinations

From: Allamarein

Date: 31 Aug, 2009 17:25:03

Message: 3 of 14

On 31 Ago, 13:56, "Matt Fig" <spama...@yahoo.com> wrote:
> See:
>
> http://www.mathworks.com/matlabcentral/fileexchange/24325
> orhttp://www.mathworks.com/matlabcentral/fileexchange/11462

You're right.
Anyway Matlab appears to be in embarrassment when it have to manage a
14x4782969 matrix (and I have 4 GB RAM).

Subject: How to write all possible combinations

From: Allamarein

Date: 31 Aug, 2009 17:33:27

Message: 4 of 14

On 31 Ago, 19:25, Allamarein <matteo.diplom...@gmail.com> wrote:
> On 31 Ago, 13:56, "Matt Fig" <spama...@yahoo.com> wrote:
>
> > See:
>
> >http://www.mathworks.com/matlabcentral/fileexchange/24325
> > orhttp://www.mathworks.com/matlabcentral/fileexchange/11462
>
> You're right.
> Anyway Matlab appears to be in embarrassment when it have to manage a
> 14x4782969 matrix (and I have 4 GB RAM).

Let's my 1st event can be a number of these:[1 2 3]
2nd event [4 7 9]
3rd event[6 2 0]
How can I write every 3^3 combinations?
Obviously it's a simplified example: I have a lot of events!

Subject: How to write all possible combinations

From: Matt Fig

Date: 31 Aug, 2009 17:39:07

Message: 5 of 14

With combinator you can use uint8 to save memory.

>> M = combinator(uint8(3),14,'p','r');
>> whos
  Name Size Bytes Class

  M 4782969x14 66961566 uint8 array

Subject: How to write all possible combinations

From: Matt Fig

Date: 31 Aug, 2009 17:52:19

Message: 6 of 14

"Matt Fig" <spamanon@yahoo.com> wrote in message <h7h1rr$siu$1@fred.mathworks.com>...
> With combinator you can use uint8 to save memory.
>
> >> M = combinator(uint8(3),14,'p','r');
> >> whos
> Name Size Bytes Class
>
> M 4782969x14 66961566 uint8 array


Sorry, that should be int8, NOT uint8.

Subject: How to write all possible combinations

From: Allamarein

Date: 31 Aug, 2009 17:58:26

Message: 7 of 14

On 31 Ago, 19:52, "Matt Fig" <spama...@yahoo.com> wrote:
> "Matt Fig" <spama...@yahoo.com> wrote in message <h7h1rr$si...@fred.mathworks.com>...
> > With combinator you can use uint8 to save memory.
>
> > >> M = combinator(uint8(3),14,'p','r');
> > >> whos
> >   Name      Size                    Bytes  Class
>
> >   M   4782969x14                 66961566  uint8 array
>
> Sorry, that should be int8, NOT uint8.

I will try:
And what about this problem?

Let's my 1st event can be a number of these:[1 2 3]
2nd event [4 7 9]
3rd event[6 2 0]
How can I write every 3^3 combinations?
Obviously it's a simplified example: I have a lot of events!

Subject: How to write all possible combinations

From: Matt Fig

Date: 31 Aug, 2009 18:12:19

Message: 8 of 14

I am not sure I understand what you mean. Is this it?

>> A = [4 7 9];
>> combs = A(combinator(3,3,'p','r'))

combs =

     4 4 4
     4 4 7
     4 4 9
     4 7 4
     4 7 7
     4 7 9
     4 9 4
     4 9 7
     4 9 9
     7 4 4
     7 4 7
     7 4 9
     7 7 4
     7 7 7
     7 7 9
     7 9 4
     7 9 7
     7 9 9
     9 4 4
     9 4 7
     9 4 9
     9 7 4
     9 7 7
     9 7 9
     9 9 4
     9 9 7
     9 9 9

Subject: How to write all possible combinations

From: Bruno Luong

Date: 31 Aug, 2009 18:19:18

Message: 9 of 14

Something like this?

ev={[1 2 3] [4 7 9] [6 2 0]}

[ev{:}]=ndgrid(ev{:});
ev=reshape(cat(length(ev)+1,ev{:}),[],length(ev))

Bruno

Subject: How to write all possible combinations

From: Allamarein

Date: 31 Aug, 2009 18:24:58

Message: 10 of 14

On 31 Ago, 20:12, "Matt Fig" <spama...@yahoo.com> wrote:
> I am not sure I understand what you mean.  Is this it?
>
> >> A = [4 7 9];
> >> combs = A(combinator(3,3,'p','r'))
>
> combs =
>
>      4     4     4
>      4     4     7
>      4     4     9
>      4     7     4
>      4     7     7
>      4     7     9
>      4     9     4
>      4     9     7
>      4     9     9
>      7     4     4
>      7     4     7
>      7     4     9
>      7     7     4
>      7     7     7
>      7     7     9
>      7     9     4
>      7     9     7
>      7     9     9
>      9     4     4
>      9     4     7
>      9     4     9
>      9     7     4
>      9     7     7
>      9     7     9
>      9     9     4
>      9     9     7
>      9     9     9

I try to explain my problem by an example.

Let's suppose to have to pick up 3 cards from 3 different decks.
You know in the first deck there are a Jack, a Queen and a King.
In the second one an Ace, 6 and 3.
In the third one 7, 4 and 5.

Therefore you can have 9 different combinations of 3 card in your
hand.
I would know which cards belong to each 3 cards combination.
Surely you cannot have a Jack and a Queen in your hand in the same
time, because they are both in the first deck and you pick only one
time in the first deck.
Likewise it happens for Ace and 6, for example.

(obviously you can use 8-9-10 instead Jack, Queen and King)

Subject: How to write all possible combinations

From: Allamarein

Date: 31 Aug, 2009 18:34:33

Message: 11 of 14

On 31 Ago, 20:24, Allamarein <matteo.diplom...@gmail.com> wrote:
> On 31 Ago, 20:12, "Matt Fig" <spama...@yahoo.com> wrote:
>
>
>
>
>
> > I am not sure I understand what you mean.  Is this it?
>
> > >> A = [4 7 9];
> > >> combs = A(combinator(3,3,'p','r'))
>
> > combs =
>
> >      4     4     4
> >      4     4     7
> >      4     4     9
> >      4     7     4
> >      4     7     7
> >      4     7     9
> >      4     9     4
> >      4     9     7
> >      4     9     9
> >      7     4     4
> >      7     4     7
> >      7     4     9
> >      7     7     4
> >      7     7     7
> >      7     7     9
> >      7     9     4
> >      7     9     7
> >      7     9     9
> >      9     4     4
> >      9     4     7
> >      9     4     9
> >      9     7     4
> >      9     7     7
> >      9     7     9
> >      9     9     4
> >      9     9     7
> >      9     9     9
>
> I try to explain my problem by an example.
>
> Let's suppose to have to pick up  3 cards from 3 different decks.
> You know in the first deck there are a Jack, a Queen and a King.
> In the second one an Ace, 6 and 3.
> In the third one 7, 4 and 5.
>
> Therefore you  can have 9 different combinations of 3 card in your
> hand.
> I would know which cards belong to each 3 cards combination.
> Surely you cannot have  a Jack and a Queen in your hand in the same
> time, because they are both in the first deck and you pick only one
> time in the first deck.
> Likewise it happens for Ace and 6, for example.
>
> (obviously you can use 8-9-10 instead Jack, Queen and King)

Yeah Bruno.
If I wanna use a matrix?
You suggest:
ev={[1 2 3] [4 7 9] [6 2 0]}
I'd like use a matrix:
A=[ 1 2 3; 4 7 9; 6 2 0];
How is it possible to modify your script?

Subject: How to write all possible combinations

From: Bruno Luong

Date: 31 Aug, 2009 18:43:20

Message: 12 of 14

> I'd like use a matrix:
> A=[ 1 2 3; 4 7 9; 6 2 0];
> How is it possible to modify your script?

Just do

ev=num2cell(A,2)

then continue the next steps.

Bruno

Subject: How to write all possible combinations

From: Allamarein

Date: 31 Aug, 2009 19:02:34

Message: 13 of 14

On 31 Ago, 20:43, "Bruno Luong" <b.lu...@fogale.findmycountry> wrote:
> > I'd like use a matrix:
> > A=[ 1 2 3; 4 7 9; 6 2 0];
> > How is it possible to modify your script?
>
> Just do
>
> ev=num2cell(A,2)
>
> then continue the next steps.
>
> Bruno

Yeah Bruno.
This is the solution.
I will try in these days, but it seems working.

Thanks

Subject: How to write all possible combinations

From: Thomas Vanaret

Date: 2 Sep, 2009 09:08:02

Message: 14 of 14

You could maybe use the Matlab "fullfact" function (Statistics Toolbox)

This function generate all possible combinaisons from N integers between 1 and M(i), with i from 1 to N.

For example, fullfact([2,3]) generates a 6 by 2 matrix. First column with integers between 1 and 2, second column with integers between 1 and 3. Each line is a possible combinaison :

>> fullfact([2,3])

ans =

     1 1
     2 1
     1 2
     2 2
     1 3
     2 3

You can then easily replace integers by other values (numbers, letters, what you want...)

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us