1

I'm trying to achieve a relatively simple matrix manipulation in MATLAB.

From two vectors, I would like to generate all the possible two-element pairs that could be produced. For example, given the following two vectors:

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

... I would hope to be able to produce the following:

c =

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

I understand that I could generate the above using an explicit loop (such as multiple repmat() operations), but my previous experience of MATLAB suggests that there probably is a built-in function that can achieve this more quickly...

Any suggestions?

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
CaptainProg
  • 5,610
  • 23
  • 71
  • 116

1 Answers1

1
a = [1 2 3]

a =

     1     2     3

>> b = [4 5 6]

b =

     4     5     6

>> c=allcomb(a,b)'

c =

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

You can find the allcomb function here

Daniel
  • 36,610
  • 3
  • 36
  • 69