19

I know it may sounds idiot, but I've been looking through the document, and couldn't find a way to combine two vectors say {a, b} and {c, d} into {{a, c}, {b, d}}.

Thanks all.

István Zachar
  • 47,032
  • 20
  • 143
  • 291
MUU
  • 193
  • 1
  • 1
  • 5

2 Answers2

26

You are treating $A$ and $B$ as column vectors and combining them column to column, but if you just bracket the two vectors, you are combining them row to row. A final Transpose will do the trick.

A = {a, b};
B = {c, d};
NotYourResult = {A,B}
Result = Transpose[{A, B}]

where NotYourResult is assigned {{a,b},{c,d}} or $\left[ {\begin{array}{cc} a & b \\ c & d \\ \end{array} } \right]$,

and Result is assigned {{a,c},{b,d}} or $\left[ {\begin{array}{cc} a & c \\ b & d \\ \end{array} } \right]$,

Frenzy Li
  • 540
  • 7
  • 15
16

Thread can be useful, too:

A = {a, b};
B = {c, d};

Thread[{A, B}]

{{a, c}, {b, d}}

The canonical Q&A for this kind of stuff probably is Elegant operations on matrix rows and columns.

Yves Klett
  • 15,383
  • 5
  • 57
  • 124