MathGroup Archive 2010

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

Search the Archive

Re: How to unflatten an array ?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg112830] Re: How to unflatten an array ?
  • From: Valeri Astanoff <astanoff at gmail.com>
  • Date: Sat, 2 Oct 2010 05:45:23 -0400 (EDT)
  • References: <i84aed$h0e$1@smc.vnet.net>

On 1 oct, 11:42, Bob Hanlon <hanl... at cox.net> wrote:
> unflatten[arr : {{_, _, _} ..}] :=
>  SplitBy[{Most[#], Last[#]} & /@ arr, #[[1, 1]] &]
>
> 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];
>
> a1 == unflatten@a2
>
> True
>
> Bob Hanlon
>
> ---- 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
>
> Please help me to something faster for large arrays.
>
> --
> Valeri Astanoff

Good day,

I have to apologize for not being very clear about what I wanted.
Seems that you, Bob, were the only one to understand what I meant.
[I had to replace SplitBy with Split cause I only have version 6]
Any way I have to thank everyone who tried and help me.
Maybe this test will more explicit than my poor previous example :

In[1]:= a1 =
 Table[{{x, y}, RandomInteger[{0, 99}]}, {x, 0, 99}, {y, 0, 49}] ;

In[2]:= Dimensions[a1]
Out[2]= {100, 50, 2}

In[3]:= a2 = Flatten /@ Flatten[a1, 1] ;

In[4]:= Dimensions[a2]
Out[4]= {5000, 3}

In[5]:= 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[6]:= (a3 = unflatten[a2];) // Timing
Out[6]= {0.532, Null}

In[7]:= Dimensions[a3]
Out[7]= {100, 50, 2}

In[8]:= a1 == a3
Out[8]= True

In[9]:= unflattenRaffy[arr_] :=
 Transpose@{arr[[All, ;; -2]], arr[[All, -1]]}

In[10]:= (a3 = unflattenRaffy[a2] ; ) // Timing
Out[10]= {0.016, Null}

In[11]:= Dimensions[a3]
Out[11]= {5000, 2}

In[12]:= a1 == a3
Out[12]= False

In[13]:= unflattenBob[arr : {{_, _, _} ..}] :=
 Split[{Most[#], Last[#]} & /@ arr, #1[[1, 1]] == #2[[1, 1]] &]

In[14]:= (a3 = unflattenBob[a2] ;) // Timing
Out[14]= {0.062, Null}

In[15]:= Dimensions[a3]
Out[15]= {100, 50, 2}

In[16]:= a1 == a3
Out[16]= True

In[17]:= unflattenMark[arr_] :=
 Partition[{{#1, #2}, #3} & @@@ arr, 3]

In[18]:= (a3 = unflattenMark[a2] ;) // Timing
Out[18]= {0.032, Null}

In[19]:= Dimensions[a3]
Out[19]= {1666, 3, 2}

In[20]:= a1 == a3
Out[20]= False

In[21]:= unflattenLeonid[arr_] :=
 Partition[Transpose[{arr[[All, {1, 2}]], arr[[All, 3]]}], 3]

In[22]:= (a3 = unflattenLeonid[a2] ;) // Timing

Out[22]= {0.015, Null}

In[23]:= Dimensions[a3]
Out[23]= {1666, 3, 2}

In[24]:= a1 == a3
Out[24]= False

In[25]:= unflattenRay[arr_] :=
 Partition[{Most@#, Last@#} & /@ arr, 3]

In[26]:= (a3 = unflattenRay[a2] ;) // Timing
Out[26]= {0.031, Null}

In[27]:= Dimensions[a3]
Out[27]= {1666, 3, 2}

In[28]:= a1 == a3
Out[28]= False

In[29]:= unflattenChris[arr_] := {{#1, #2}, #3} & @@@ # & /@
 Partition[arr, 3]

In[30]:= (a3 = unflattenChris[a2] ;) // Timing

Out[30]= {0.031, Null}

In[31]:= Dimensions[a3]
Out[31]= {1666, 3, 2}

In[32]:= a1 == a3
Out[32]= False

In[33]:= unflattenMukasa[arr_] :=
 Partition[{Most[#], Last[#]} & /@ arr, 3]

In[34]:= (a3 = unflattenMukasa[a2] ;) // Timing
Out[34]= {0.047, Null}

In[35]:= Dimensions[a3]
Out[35]= {1666, 3, 2}

In[36]:= a1 == a3
Out[36]= False

Thanks again for your help.

--
Valeri




  • Prev by Date: Re: Mathematica "Fresh Start" on a new computer?
  • 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 ?