RE: unpartition
- To: mathgroup at smc.vnet.net
- Subject: [mg45954] RE: [mg45899] unpartition
- From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
- Date: Fri, 30 Jan 2004 04:17:09 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
>-----Original Message----- >From: steven.burger at duke.edu [mailto:steven.burger at duke.edu] To: mathgroup at smc.vnet.net >Sent: Thursday, January 29, 2004 11:35 AM >To: mathgroup at smc.vnet.net >Subject: [mg45954] [mg45899] unpartition > > >How do you unpartition a matrix? > >For example: Starting with, > >Partition[Table[4*(i - 1) + j, {i, 4}, {j, 4}],{2,2}] > > >how do you go back to, > >Table[4*(i - 1) + j, {i, 4}, {j, 4}] > In principle, the inverse to Partition is Flatten. The problem is to get the dimensions right! Lets first consider your example: In[15]:= Table[4*(i - 1) + j, {i, 4}, {j, 4}] Out[15]= {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}} In[16]:= Partition[%, {2, 2}] Out[16]= {{{{1, 2}, {5, 6}}, {{3, 4}, {7, 8}}}, {{{9, 10}, {13, 14}}, {{11, 12}, {15, 16}}}} In[17]:= Dimensions[%] Out[17]= {2, 2, 2, 2} In[18]:= Transpose[%%, {1, 3, 2, 4}] Out[18]= {{{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}}, {{{9, 10}, {11, 12}}, {{13, 14}, {15, 16}}}} In[19]:= Map[Flatten, %, {2}] Out[19]= {{{1, 2, 3, 4}, {5, 6, 7, 8}}, {{9, 10, 11, 12}, {13, 14, 15, 16}}} In[20]:= Flatten[%, 1] Out[20]= {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}} This sequence works in general: In[25]:= {k, l, m, n} = Prime /@ Table[Random[Integer, {1, 10}], {4}] Out[25]= {3, 29, 11, 2} In[26]:= t = Partition[Range[k*l*m*n], k*l]; In[27]:= Dimensions[t] == {m*n, k*l} Out[27]= True In[28]:= s = Partition[t, {m, k}]; In[29]:= Dimensions[s] == {n, l, m, k} Out[29]= True In[30]:= ss = Transpose[s, {1, 3, 2, 4}]; In[31]:= Dimensions[ss] == {n, m, l, k} Out[31]= True In[32]:= t == Flatten[Map[Flatten, ss, {2}], 1] Out[32]= True or also In[33]:= s2 = Partition[t, {n, l}]; In[34]:= Dimensions[s2] == {m, k, n, l} Out[34]= True In[35]:= ss2 = Transpose[s2, {1, 3, 2, 4}]; In[36]:= Dimensions[ss2] == {m, n, k, l} Out[36]= True In[37]:= t == Flatten[Map[Flatten, ss2, {2}], 1] Out[37]= True Alternatively you may Flatten everything and then repartition with the appropriate dimensions: In[45]:= t == Partition[Flatten[Transpose[s, {1, 3, 2, 4}]], Times @@ Dimensions[s][[{2, 4}]]] Out[45]= True In[46]:= t == Partition[Flatten[Transpose[s2, {1, 3, 2, 4}]], Times @@ Dimensions[s2][[{2, 4}]]] Out[46]= True -- Hartmut Wolf