Re: How to unflatten an array ?
- To: mathgroup at smc.vnet.net
- Subject: [mg112851] Re: How to unflatten an array ?
- From: Valeri Astanoff <astanoff at gmail.com>
- Date: Sun, 3 Oct 2010 05:40:58 -0400 (EDT)
- References: <i84aed$h0e$1@smc.vnet.net> <i86uvo$hpk$1@smc.vnet.net> <i89btu$jf6$1@smc.vnet.net>
On 3 oct, 09:38, Ray Koopman <koop... at sfu.ca> wrote: > On Oct 2, 2:45 am, Valeri Astanoff <astan... at gmail.com> wrote: > > > > > 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 : > > [...] > > I think I understand now what you're doing. > > In[1]:= Dimensions[a1 = Table[{{x,y}, RandomInteger[99]}, > {x, 0, 99}, {= y, 0, 99}]] > Out[1]= {100, 100, 2} > > In[2]:= Dimensions[a2 = Flatten /@ Flatten[a1, 1]] > > Out[2]= {10000, 3} > > In[3]:= 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[4]:= {Timing@Dimensions[a3 = unflatten[a2]], a3 == a1} > > Out[4]= {{1.24, {100, 100, 2}}, True} > > In[5]:= unflattenBob[arr : {{_, _, _} ..}] := Split[ > {Most[#],Last[#]}& /@ arr, #1[[1,1]] == #2[[1,1]] = &] > > In[6]:= {Timing@Dimensions[a3 = unflattenBob[a2]], a3 == a1} > > Out[6]= {{0.12, {100, 100, 2}}, True} > > In[7]:= unflattenRay[arr_] := Partition[Transpose@ > {arr[[All,{1,2}]],arr[[All,3]]},Length@Union@arr[[All,= 2]]] > > In[8]:= {Timing@Dimensions[a3 = unflattenRay[a2]], a3 == a1} > > Out[8]= {{0.02, {100, 100, 2}}, True} Good day, My "unflatten" was a tortoise, your own is a hare ! This is going to be very useful. Many thanks (to you and other posters). -- Valeri