From: Sam Takoy on
Hi,

Given two matrices of equal height, what's the best way to combine them.
Here's what I did

TF[m_] := Flatten[Transpose[m]]
Combine[m1_, m2_] :=
Partition[Join[m1 // TF, m2 // TF], Length[m1]] // T


Surely there's a better way of doing it.

Thanks!

From: David Park on
Here are two sample matrices.

m1 = Array[1 &, {3, 2}];
m2 = Array[2 &, {3, 3}];

Transpose[Join[Transpose[m1], Transpose[m2]]] // MatrixForm

Another method that doesn't explicitly use Join and Transpose is to make a
new blank matrix first and then fill in the parts using the Span notation.
This might be more intuitive.

m3 = Array[0 &, {3, 5}];
m3[[All, 1 ;; 2]] = m1;
m3[[All, 3 ;; 5]] = m2;
m3 // MatrixForm


David Park
djmpark(a)comcast.net
http://home.comcast.net/~djmpark/


From: Sam Takoy [mailto:sam.takoy(a)yahoo.com]


Hi,

Given two matrices of equal height, what's the best way to combine them.
Here's what I did

TF[m_] := Flatten[Transpose[m]]
Combine[m1_, m2_] :=
Partition[Join[m1 // TF, m2 // TF], Length[m1]] // T


Surely there's a better way of doing it.

Thanks!



From: Leonid Shifrin on
Hi,

you did not provide a test example and your code does not seem to work for me so
I can only guess what you meant by combining. The following function will
combine together any number of matrices provided they all have the same length (number of rows):

combine[x___?MatrixQ /; Equal @@ Map[Length, {x}]]
:= Flatten[Transpose[{x}], {{1}, {2, 3}}];

For example:

In[19]:= tst1 = Partition[Range[12], 3]

Out[19]= {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}}

In[20]:= tst2 = Partition[Range[13, 24], 3]

Out[20]= {{13, 14, 15}, {16, 17, 18}, {19, 20, 21}, {22, 23, 24}}

In[21]:= combine[tst1, tst2]

Out[21]= {{1, 2, 3, 13, 14, 15}, {4, 5, 6, 16, 17, 18}, {7, 8, 9, 19,
20, 21}, {10, 11, 12, 22, 23, 24}}

Hope this helps.

Regards,
Leonid


On Sat, Jul 17, 2010 at 4:16 PM, Sam Takoy <sam.takoy(a)yahoo.com> wrote:

> Hi,
>
> Given two matrices of equal height, what's the best way to combine them.
> Here's what I did
>
> TF[m_] := Flatten[Transpose[m]]
> Combine[m1_, m2_] :=
> Partition[Join[m1 // TF, m2 // TF], Length[m1]] // T
>
>
> Surely there's a better way of doing it.
>
> Thanks!
>
>
From: Bill Rowe on
On 7/17/10 at 8:16 AM, sam.takoy(a)yahoo.com (Sam Takoy) wrote:

>Given two matrices of equal height, what's the best way to combine
>them. Here's what I did

>TF[m_] := Flatten[Transpose[m]] Combine[m1_, m2_] :=
>Partition[Join[m1 // TF, m2 // TF], Length[m1]] // T

I assume there is a typo here and the last portion of what you
wrote above should be TF not just T.

If so, joining two matrices can be done with ArrayFlatten.
Taking your "equal height" to mean an equal number of rows, then

=46latten(a)ArrayFlatten@{{m1,m2}}

gives the same result as your code. For example,

In[12]:= a = RandomInteger[1, {5, 2}];
b = RandomInteger[5, {5, 3}];
Combine[a, b] == Flatten(a)ArrayFlatten@{{a, b}}

Out[14]= True

Note, what I have done with ArrayFlatten requires the two
matrices to have the same number of rows which is not true of
your code. You can relax this requirement by using Riffle which
will not care if the to matrices have the same number of rows.
That is:

In[15]:= Combine[a, b] == Flatten(a)Riffle[a, b]

Out[15]= True


From: Ray Koopman on
On Jul 17, 5:16 am, Sam Takoy <sam.ta...(a)yahoo.com> wrote:
> Hi,
>
> Given two matrices of equal height, what's the best way to combine them.
> Here's what I did
>
> TF[m_] := Flatten[Transpose[m]]
> Combine[m1_, m2_] :=
> Partition[Join[m1 // TF, m2 // TF], Length[m1]] // T
>
> Surely there's a better way of doing it.
>
> Thanks!

m1 = {{11,12},
{21,22}};
m2 = {{13,14,15},
{23,24,25}};
m3 = {{16,17},
{26,27}};
MapThread[Join,{m1,m2,m3}]

{{11,12,13,14,15,16,17},
{21,22,23,24,25,26,27}}