Discussion:
Combine matrices of equal height
(too old to reply)
Sam Takoy
2010-07-17 12:16:34 UTC
Permalink
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!
David Park
2010-07-18 05:02:18 UTC
Permalink
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
***@comcast.net
http://home.comcast.net/~djmpark/


From: Sam Takoy [mailto:***@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!
Leonid Shifrin
2010-07-18 05:03:54 UTC
Permalink
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
Post by Sam Takoy
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!
Bill Rowe
2010-07-18 05:04:48 UTC
Permalink
Post by Sam Takoy
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

=***@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] == ***@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] == ***@Riffle[a, b]

Out[15]= True
Ray Koopman
2010-07-18 05:05:21 UTC
Permalink
Post by Sam Takoy
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}}
Bob Hanlon
2010-07-18 05:06:36 UTC
Permalink
TF[m_] := Flatten[Transpose[m]]

Combine[m1_, m2_] :=
Partition[Join[m1 // TF, m2 // TF], Length[m1]] // TF

Combine2[m1_, m2_] :=
Flatten[Transpose[Join[Transpose[m1], Transpose[m2]]]]

Combine3[m1_, m2_] := Flatten[Riffle[m1, m2]]

m1 = Array[x, {3, 4}];

m2 = Array[y, {3, 4}];

Combine[m1, m2] == Combine2[m1, m2] == Combine3[m1, m2]

True


Bob Hanlon

---- Sam Takoy <***@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!
Murray Eisenberg
2010-07-18 05:07:08 UTC
Permalink
First, unless I've missed something, it's not so easy find out how to do
this by searching the documentation.

Second, I always hesitate to deem a method "the best way" to do
something. But Join with a third argument designating joining at "level"
2 does the trick nicely.

This is documented twice on page ref/Join, first in the Scope section
and then again in the Applications section. Example, like those:

m1 = {{a,b,c},{d,e,f},{g,h,i}};
m2 = {{x,y}, {u,v}, {z,w}};
wider = Join[m1, m2, 2]

Thus there is no need to resort to the "trick" of using Transpose.

Third, the difficulty here is, to my mind, a limitation of Mathematica's
inherent lack of any true array structure but rather just the
one-dimensional structure of lists. Higher-order arrays need to be built
more or less explicitly from lists. And that means the seemingly
artificial way of treating questions such as yours, by means of a
"level" specification.

In a language that has a full-fledged development of arrays of any order
(e.g., APL and, especially, J), such things as you ask are often easier,
and more general, to handle.
Post by Sam Takoy
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!
--
Murray Eisenberg ***@math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower phone 413 549-1020 (H)
University of Massachusetts 413 545-2859 (W)
710 North Pleasant Street fax 413 545-1801
Amherst, MA 01003-9305
Continue reading on narkive:
Loading...