Re: How to unflatten an array ?

• To: mathgroup at smc.vnet.net
• Subject: [mg112815] Re: How to unflatten an array ?
• From: Chris Degnen <degnen at cwgsy.net>
• Date: Fri, 1 Oct 2010 05:43:19 -0400 (EDT)
• References: <i81j69\$9v8\$1@smc.vnet.net>

```On 30 Sep, 09:53, Valeri Astanoff <astan... 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
>
>
> --
> Valeri Astanoff

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}]];
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}}};
a2 = Flatten /@ Flatten[a1, 1]
a3 = unflatten@a2
a1 == a3
(* Alternative method *)
a3 = {{#1, #2}, #3} & @@@ # & /@ Partition[a2, 3]
a1 == a3
(* Timing tests *)
a2Large = Flatten[ConstantArray[a2, 1000], 1];
a2LargeTime = Part[Timing[unflatten@a2Large], 1]
a2LargeTime =
Part[Timing[{{#1, #2}, #3} & @@@ # & /@ Partition[a2Large, 3]], 1]

```

• Prev by Date: Re: 4 Gb are not enough ...
• Next by Date: Re: Help to solve an integral by using Mathematica Integrate[Sqrt[t
• Previous by thread: Re: How to unflatten an array ?
• Next by thread: Re: How to unflatten an array ?