Re: How to unflatten an array ?
- To: mathgroup at smc.vnet.net
- Subject: [mg112813] Re: How to unflatten an array ?
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Fri, 1 Oct 2010 05:42:56 -0400 (EDT)
Hi Valeri, It is not clear to me how your sublists are partitioned within the big list in the unflattened version - always by 3 together, or some other principle? Your function seems to work by pure accident. Consider random data of your form instead of your a1: In[50]:== rnda == Partition[Table[{{RandomInteger[{0,10}],RandomInteger[{0,10}]},RandomIntege= r[{0,10}]},{9}],3] Out[50]== {{{{8,8},7},{{2,1},7},{{3,1},7}},{{{10,9},1},{{8,9},10},{{10,5},8}},{{{9,10= },6},{{9,8},1},{{7,6},1}}} In[51]:== rnda2 == Flatten/@Flatten[rnda,1] Out[51]== {{8,8,7},{2,1,7},{3,1,7},{10,9,1},{8,9,10},{10,5,8},{9,10,6},{9,8,1},{7,6,1= }} In[52]:== unflatten@rnda2 Out[52]== {{{{2,1},7},{{2,5},f$2163[2,5]},{{2,6},f$2163[2,6]},{{2,8},f$2163[2,8]},{{2= ,9},f$2163[2,9]},{{2,10},f$2163[2,10]}},{{{3,1},7},{{3,5},f$2163[3,5]},{{3,= 6},f$2163[3,6]},{{3,8},f$2163[3,8]},{{3,9},f$2163[3,9]},{{3,10},f$2163[3,10= ]}},{{{7,1},f$2163[7,1]},{{7,5},f$2163[7,5]},{{7,6},1},{{7,8},f$2163[7,8]},= {{7,9},f$2163[7,9]},{{7,10},f$2163[7,10]}},{{{8,1},f$2163[8,1]},{{8,5},f$21= 63[8,5]},{{8,6},f$2163[8,6]},{{8,8},7},{{8,9},10},{{8,10},f$2163[8,10]}},{{= {9,1},f$2163[9,1]},{{9,5},f$2163[9,5]},{{9,6},f$2163[9,6]},{{9,8},1},{{9,9}= ,f$2163[9,9]},{{9,10},6}},{{{10,1},f$2163[10,1]},{{10,5},8},{{10,6},f$2163[= 10,6]},{{10,8},f$2163[10,8]},{{10,9},1},{{10,10},f$2163[10,10]}}} So, it gives a mess here. Either it uses some data format-specific information in some non-obvious way (but then you had to give us a more precise format specification for you unflattened list), - and then my randomly generated data simply do not comply with your format, or your function is wrong. Assuming constant partitioning by 3 sublists in the main unflattened list, here is my attempt: In[61]:== a1 == {{{{1, 0}, 1}, {{1, 1}, 1}, {{1, 2}, 3}}, {{{2, 0}, 5}, {{2, 1}, 0}, {{2, 2}, 0}}, {{{3, 0}, 4}, {{3, 1}, 5}, {{3, 2}, 2}}, {{{5, 0}, 1}, {{5, 1}, 3}, {{5, 2}, 2}}}; In[62]:== a3 == Partition[Transpose[{a1fl[[All, {1, 2}]], a1fl[[All, 3]]}], 3] Out[62]== {{{{1, 0}, 1}, {{1, 1}, 1}, {{1, 2}, 3}}, {{{2, 0}, 5}, {{2, 1}, 0}, {{2, 2}, 0}}, {{{3, 0}, 4}, {{3, 1}, 5}, {{3, 2}, 2}}, {{{5, 0}, 1}, {{5, 1}, 3}, {{5, 2}, 2}}} In[63]:== a3 ====== a1 Out[63]== True Regards, Leonid On Thu, Sep 30, 2010 at 12:52 PM, Valeri Astanoff <astanoff at gmail.com>wrote= : > Good day, > > Suppose I have a flat array like this : > > {{1, 0, 1}, {1, 1, 1}, {1, 2, 3}, {2, 0, 5}... > > and I want to get back to this "unflattened" form : > > {{{{1, 0}, 1}, {{1, 1}, 1}, {{1, 2}, 3}}, {{{2, 0}, 5}... > > What is the most efficient way to do it ? > > > All I have found is this : > > In[1]:== unflatten[arr : {{_, _, _} ..}] :== > Module[{f}, > Scan[(f[#[[1]], #[[2]]] == #[[3]]) &, arr]; > Table[{{x, y}, f[x, y]}, > {x, arr[[All, 1]] // Union}, > {y, arr[[All, 2]] // Union}] > ]; > > In[2]:== a1 == {{{{1, 0}, 1}, {{1, 1}, 1}, {{1, 2}, 3}}, > {{{2, 0}, 5}, {{2, 1}, 0}, {{2, 2}, 0}}, {{{3, 0}, 4}, > {{3, 1}, 5}, {{3, 2}, 2}}, {{{5, 0}, 1}, {{5, 1}, 3}, > {{5, 2}, 2}}}; > > In[3]:== a2 == Flatten /@ Flatten[a1, 1] > > Out[3]== {{1, 0, 1}, {1, 1, 1}, {1, 2, 3}, {2, 0, 5}, > {2, 1, 0}, {2, 2, 0}, {3, 0, 4}, {3, 1, 5}, {3, 2, 2}, > {5, 0, 1}, {5, 1, 3}, {5, 2, 2}} > > In[4]:== a3 == unflatten@a2 > > Out[4]== {{{{1, 0}, 1}, {{1, 1}, 1}, {{1, 2}, 3}}, > {{{2, 0}, 5}, {{2, 1}, 0}, {{2, 2}, 0}}, {{{3, 0}, 4}, > {{3, 1}, 5}, {{3, 2}, 2}}, {{{5, 0}, 1}, {{5, 1}, 3}, > {{5, 2}, 2}}} > > In[5]:== a1 ==== a3 > > Out[5]== True > > > Please help me to something faster for large arrays. > > -- > Valeri Astanoff > >