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:
Combinations of several vectors

Subject: Combinations of several vectors

From: Eyal Fleminger

Date: 21 Apr, 2010 11:45:22

Message: 1 of 3

Hello

Say I have several vectors, e.g.:

a=[1,2]
b=[3,4]
c=[5,6]

I am looking for a way to create a matrix, each line of which is one combination of the values in the input vectors. So for the three vectors abov, I would get:

1 3 5
1 3 6
1 4 5
1 4 6
2 3 5
2 3 6
2 4 5
2 4 6

(the order of the combinations doesn't matter).

For two vectors, it's pretty simple to do this with repmat and sort:

[sort(repmat(a(:),length(b),1)),repmat(b(:),length(a),1)]

However, that gets increasingly more complicated when more than two vectors are input. Does anyone have an idea for a simpler way to do this?

Subject: Combinations of several vectors

From: Roger Stafford

Date: 21 Apr, 2010 12:07:05

Message: 2 of 3

"Eyal Fleminger" <efleminge@gmail.com> wrote in message <hqmogi$rui$1@fred.mathworks.com>...
> Hello
>
> Say I have several vectors, e.g.:
>
> a=[1,2]
> b=[3,4]
> c=[5,6]
>
> I am looking for a way to create a matrix, each line of which is one combination of the values in the input vectors. So for the three vectors abov, I would get:
>
> 1 3 5
> 1 3 6
> 1 4 5
> 1 4 6
> 2 3 5
> 2 3 6
> 2 4 5
> 2 4 6
>
> (the order of the combinations doesn't matter).
>
> For two vectors, it's pretty simple to do this with repmat and sort:
>
> [sort(repmat(a(:),length(b),1)),repmat(b(:),length(a),1)]
>
> However, that gets increasingly more complicated when more than two vectors are input. Does anyone have an idea for a simpler way to do this?

 [A,B,C] = ndgrid(a,b,c);
 X = [A(:),B(:),C(:)];

Roger Stafford

Subject: Combinations of several vectors

From: Jos (10584)

Date: 21 Apr, 2010 13:19:05

Message: 3 of 3

"Eyal Fleminger" <efleminge@gmail.com> wrote in message <hqmogi$rui$1@fred.mathworks.com>...
> Hello
>
> Say I have several vectors, e.g.:
>
> a=[1,2]
> b=[3,4]
> c=[5,6]
>
> I am looking for a way to create a matrix, each line of which is one combination of the values in the input vectors. So for the three vectors abov, I would get:
>
> 1 3 5
> 1 3 6
> 1 4 5
> 1 4 6
> 2 3 5
> 2 3 6
> 2 4 5
> 2 4 6
>
> (the order of the combinations doesn't matter).
>
> For two vectors, it's pretty simple to do this with repmat and sort:
>
> [sort(repmat(a(:),length(b),1)),repmat(b(:),length(a),1)]
>
> However, that gets increasingly more complicated when more than two vectors are input. Does anyone have an idea for a simpler way to do this?

Take a look at ALLCOMB

a=[1,2], b=[3,4], c=[5,6]

M = allcomb(a,b,c)

ALLCOMB can be downloaded from the File Exchange:
http://www.mathworks.com/matlabcentral/fileexchange/10064-allcomb

hth
Jos

Tags for this Thread

No tags are associated with 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