|
[Date Index]
[Thread Index]
[Author Index]
Re: opposite of partition
- To: mathgroup at smc.vnet.net
- Subject: [mg57632] Re: [mg57539] opposite of partition
- From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
- Date: Thu, 2 Jun 2005 05:17:34 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
>-----Original Message-----
>From: Guy Israeli [mailto:guyi1 at netvision.net.il]
To: mathgroup at smc.vnet.net
>Sent: Tuesday, May 31, 2005 11:01 AM
>Subject: [mg57632] [mg57539] opposite of partition
>
>Hi,
>
>How do I do the opposite of partition quickly?
>
>for example:
>
>l1= {{a, b, c, d, e}, {f, g, h, i, j}, {k, l, m, n, o}, {p, q,
>r, s, t}, {u,
>v, w,
>x, y}}
>
>and then if I partition it to blocks will result in
>
>{{{{a, b}, {f, g}}, {{c, d}, {h, i}}}, {{{k, l}, {p, q}}, {{m,
>n}, {r, s}}}}
>
>flattening it won't help, and its messy to do it by taking all
>first lines
>of the blocks, then second..
>
>How can I do it quickly?
>
>
>
>Thanks,
>
>Guy
>
>
>
The opposite of Partition essentially is Join, but we have to understand
first what Partition does for nested Lists.
This one is easy:
In[2]:=
l0 = {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z};
In[3]:= l1 = Partition[l0,5]
Out[3]=
{{a, b, c, d, e}, {f, g, h, i, j}, {k, l, m, n, o}, {p, q, r, s, t},
{u, v, w, x, y}}
In[4]:= Apply[Join,l1,{0}]
Out[4]=
{a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x,
y}
Clearly you won't get back all of l0
Now for your example:
In[5]:= l2=Partition[l1, {2,2}]
Out[5]=
{{{{a,b},{f,g}},{{c,d},{h,i}}},{{{k,l},{p,q}},{{m,n},{r,s}}}}
We have to understand what Partition does here.
First it partitions at top level:
In[6]:= Map[Partition[#1, 2]&, l1, {0}]
Out[6]=
{{{a,b,c,d,e},{f,g,h,i,j}},{{k,l,m,n,o},{p,q,r,s,t}}}
Then at next level, but which now (due to the first application is not 1
but 1+1 == 2)
In[7]:= Map[Partition[#1, 2]&, %, {2}]
Out[7]=
{{{{a,b},{c,d}},{{f,g},{h,i}}},{{{k,l},{m,n}},{{p,q},{r,s}}}}
We can fold both operations to get this:
In[8]:=
Fold[Map[Function[s,Partition[s, First[#2]]], #1, Rest[#2]]&, l1,
Transpose[{{2,2},{0,2}}]]
Out[8]=
{{{{a,b},{c,d}},{{f,g},{h,i}}},{{{k,l},{m,n}},{{p,q},{r,s}}}}
But we have in addition to transpose the levels (in a characteristic
fashion):
In[9]:= Transpose[%,{1,3,2,4}]
Out[9]=
{{{{a,b},{f,g}},{{c,d},{h,i}}},{{{k,l},{p,q}},{{m,n},{r,s}}}}
In[10]:= %==l2
Out[10]= True
So now, as we know the operation, we can reverse each step:
In[11]:= l3=Transpose[l2,{1,3,2,4}]
Out[11]=
{{{{a, b}, {c, d}}, {{f, g}, {h, i}}}, {{{k, l}, {m, n}}, {{p, q}, {r,
s}}}}
In[12]:= Fold[Apply[Join,##]&,l3,{{2},{0}}]
Out[12]=
{{a, b, c, d}, {f, g, h, i}, {k, l, m, n}, {p, q, r, s}}
This is the best we can restore from original List l1.
As this example is a bit special, we make up a more general one:
In[20]:= j1=Fold[Partition[#1,#2]&,Range[200],{7,3}]
Out[20]=
{{{1, 2, 3, 4, 5, 6, 7}, {8, 9, 10, 11, 12, 13, 14},
{15, 16, 17, 18, 19, 20, 21}},
{{22, 23, 24, 25, 26, 27, 28}, {29, 30, 31, 32, 33, 34, 35},
{36, 37, 38, 39, 40, 41, 42}},
{{43, 44, 45, 46, 47, 48, 49}, {50, 51, 52, 53, 54, 55, 56},
{57, 58, 59, 60, 61, 62, 63}},
{{64, 65, 66, 67, 68, 69, 70}, {71, 72, 73, 74, 75, 76, 77},
{78, 79, 80, 81, 82, 83, 84}},
{{85, 86, 87, 88, 89, 90, 91}, {92, 93, 94, 95, 96, 97, 98},
{99, 100, 101, 102, 103, 104, 105}},
{{106, 107, 108, 109, 110, 111, 112}, {113, 114, 115, 116, 117, 118,
119},
{120, 121, 122, 123, 124, 125, 126}},
{{127, 128, 129, 130, 131, 132, 133}, {134, 135, 136, 137, 138, 139,
140},
{141, 142, 143, 144, 145, 146, 147}},
{{148, 149, 150, 151, 152, 153, 154}, {155, 156, 157, 158, 159, 160,
161},
{162, 163, 164, 165, 166, 167, 168}},
{{169, 170, 171, 172, 173, 174, 175}, {176, 177, 178, 179, 180, 181,
182},
{183, 184, 185, 186, 187, 188, 189}}}
In[21]:=
j2=Partition[j1,{4,2,3}]
Out[21]=
{{{{{{1, 2, 3}, {8, 9, 10}}, {{22, 23, 24}, {29, 30, 31}},
{{43, 44, 45}, {50, 51, 52}}, {{64, 65, 66}, {71, 72, 73}}},
{{{4, 5, 6}, {11, 12, 13}}, {{25, 26, 27}, {32, 33, 34}},
{{46, 47, 48}, {53, 54, 55}}, {{67, 68, 69}, {74, 75, 76}}}}},
{{{{{85, 86, 87}, {92, 93, 94}}, {{106, 107, 108}, {113, 114, 115}},
{{127, 128, 129}, {134, 135, 136}}, {{148, 149, 150}, {155, 156,
157}}},
{{{88, 89, 90}, {95, 96, 97}}, {{109, 110, 111}, {116, 117, 118}},
{{130, 131, 132}, {137, 138, 139}}, {{151, 152, 153}, {158, 159,
160}}}}}}
Again we have to transpose the deeper levels:
In[22]:= Transpose[j2,{1,3,5,2,4,6}]
Then iterate Join-ing upwards
In[23]:= Fold[Apply[Join,##]&,%,{{4},{2},{0}}]
Out[23]=
{{{1, 2, 3, 4, 5, 6}, {8, 9, 10, 11, 12, 13}},
{{22, 23, 24, 25, 26, 27}, {29, 30, 31, 32, 33, 34}},
{{43, 44, 45, 46, 47, 48}, {50, 51, 52, 53, 54, 55}},
{{64, 65, 66, 67, 68, 69}, {71, 72, 73, 74, 75, 76}},
{{85, 86, 87, 88, 89, 90}, {92, 93, 94, 95, 96, 97}},
{{106, 107, 108, 109, 110, 111}, {113, 114, 115, 116, 117, 118}},
{{127, 128, 129, 130, 131, 132}, {134, 135, 136, 137, 138, 139}},
{{148, 149, 150, 151, 152, 153}, {155, 156, 157, 158, 159, 160}}
Again, this is the best we can restore from j1, compare.
--
Hartmut Wolf
Prev by Date:
Re: Re: Limit of list
Next by Date:
Re: Re: Re: making an animated picture from many pictures
Previous by thread:
Re: opposite of partition
Next by thread:
Re: Re: Limit of list
|