Re: How to unflatten an array ?
- To: mathgroup at smc.vnet.net
- Subject: [mg112845] Re: How to unflatten an array ?
- From: Ray Koopman <koopman at sfu.ca>
- Date: Sun, 3 Oct 2010 03:38:21 -0400 (EDT)
- References: <i84aed$h0e$1@smc.vnet.net> <i86uvo$hpk$1@smc.vnet.net>
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}