On 7/17/10 at 8:16 AM, sam.takoy at 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@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@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@Riffle[a, b]
Out[15]= True