Re: Combine matrices of equal height
- To: mathgroup at smc.vnet.net
- Subject: [mg111043] Re: Combine matrices of equal height
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Sun, 18 Jul 2010 01:03:53 -0400 (EDT)
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 at 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!
>
>