MathGroup Archive 1997

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Joining matrices

  • To: mathgroup at smc.vnet.net
  • Subject: [mg5758] Re: [mg5738] Joining matrices
  • From: Peder Thusgaard Ruhoff <ptk at dit.ou.dk>
  • Date: Tue, 14 Jan 1997 10:42:32 -0500
  • Sender: owner-wri-mathgroup at wolfram.com

On Sat, 11 Jan 1997, Jean-Francois Alcover wrote:

> Date: Sat, 11 Jan 1997 14:29:22 -0500
> From: Jean-Francois Alcover <70763.411 at compuserve.com>
To: mathgroup at smc.vnet.net
> To: mathgroup at smc.vnet.net
> Subject: [mg5758] [mg5738] Joining matrices
> 
> i have to join 2 or more matrices line by line
> for example : joinmat[{{{1,2},{3,4}},{{10,20},{30,40}}}] 
>                  --> {{1,2,10,20},{3,4,30,40}}
> 
> i have tried Transpose [Flatten[Transpose /@ ListOfMatrices,1]]
> but it seems rather slow
> 
> can anyone help me with a faster solution ?
> 

Dear Jean-Francois Alcover,



First I give an example of your function for joining matrices

-------------------------------------------------------------
Mathematica 2.2 for SGI
Copyright 1988-94 Wolfram Research, Inc.
 -- Motif graphics initialized --

In[1]:= YourJoinMatrices[ms_] := 
            Transpose [Flatten[Transpose /@ ms,1]]

In[2]:= listofmatrices = {{{1,2},{3,4}},{{10,20},{30,40}}};

In[3]:= MatrixForm /@ listofmatrices

Out[3]= {1   2, 10   20}

         3   4  30   40

In[4]:= YourJoinMatrices[listofmatrices] // MatrixForm

Out[4]//MatrixForm= 1    2    10   20

                    3    4    30   40
-------------------------------------------------------------

In the following I give an alternative to your function
for joining matrices

-------------------------------------------------------------
In[5]:= JoinMatrices[ms_] := MapThread[Join, ms]

In[6]:= JoinMatrices[listofmatrices] // MatrixForm

Out[6]//MatrixForm= 1    2    10   20

                    3    4    30   40
-------------------------------------------------------------

Note that in the above Join might be replaced by
Flatten[{##}, 1]& if you prefer using Flatten instead of
Join.

Let us now compare the two definitions with respect to 
execution time

-------------------------------------------------------------
In[7]:= listofmatrices = Array[#, {100, 100}]& /@ Array[f, 10];

In[8]:= Timing[Do[YourJoinMatrices[listofmatrices],{10}]]

Out[8]= {6.69 Second, Null}

In[9]:= Timing[Do[JoinMatrices[listofmatrices],{10}]]

Out[9]= {2.49 Second, Null}
-------------------------------------------------------------

Another test

-------------------------------------------------------------
In[10]:= listofmatrices = Table[Random[], {2000}, {3}, {3}];

In[11]:= Timing[Do[YourJoinMatrices[listofmatrices],{10}]]

Out[11]= {1.65 Second, Null}

In[12]:= Timing[Do[JoinMatrices[listofmatrices],{10}]]

Out[12]= {0.53 Second, Null}
-------------------------------------------------------------

Hope this helps.

Peder

-------------------------------------+--------------------------------------
Peder Thusgaard Ruhoff, Ph.D.        | Phone:  (+45) 65 57 35 43
Department of Information Technology | Fax:    (+45) 66 15 76 97 
Odense University                    | 
Campusvej 55                         | E-mail: ptk at dit.ou.dk
DK-5230 Odense M, DENMARK            | URL:    http://www.dit.ou.dk/~ptk/
-------------------------------------+--------------------------------------

"It is important for him who wants to discover not to confine himself to one 
 chapter of science, but to keep in touch with various others" - J. Hadamard





  • Prev by Date: Re: Image Processing: Mathematica?
  • Next by Date: Question: Purchase of Mathematica
  • Previous by thread: Re: Joining matrices
  • Next by thread: Joining matrices