5

Suppose I have multiple lists, the first one contains 2 elements, the next one 4 elements, the next one 8 elements, and so forth.

Let these elements be labeled as Gij where i refers to the list it comes from and j refers to the position it occupies in that list.

How would I go about generating the following recursive sequence.

{G11, G12, G11 + G21, G11 + G22, G12 + G23, G12 + G24, G11 + G21 + G31, G11 + G21 + G32, G11 + G22 + G33, G11 + G22 + G34, G12 + G23 + G35, G12 + G23 + G36, G12 + G24 + G37, G12 + G24 + G38, ...}

MarcoB
  • 67,153
  • 18
  • 91
  • 189
Ibrahim
  • 169
  • 5
  • Flatten@FoldList[ Flatten@MapThread[ Outer[Plus, {#1}, #2] &, {#1, Partition[#2, 2]}] &, lists] where lists is the list of your lists... – ciao Jun 27 '15 at 23:08
  • 2
    Why the downvotes? This is not such a trivial task... – ciao Jun 27 '15 at 23:15

2 Answers2

6

Example data:

lists = Table[g[i, j], {i, 1, 3}, {j, 1, 2^i}];

munging:

results=Flatten@FoldList[Flatten@MapThread[
     Outer[Plus, {#1}, #2] &, {#1, Partition[#2, 2]}] &, lists];

lists
results

(*

{{g[1, 1], g[1, 2]}, {g[2, 1], g[2, 2], g[2, 3], g[2, 4]}, {g[3, 1], 
  g[3, 2], g[3, 3], g[3, 4], g[3, 5], g[3, 6], g[3, 7], g[3, 8]}}

{g[1, 1], g[1, 2], g[1, 1] + g[2, 1], g[1, 1] + g[2, 2], 
 g[1, 2] + g[2, 3], g[1, 2] + g[2, 4], g[1, 1] + g[2, 1] + g[3, 1], 
 g[1, 1] + g[2, 1] + g[3, 2], g[1, 1] + g[2, 2] + g[3, 3], 
 g[1, 1] + g[2, 2] + g[3, 4], g[1, 2] + g[2, 3] + g[3, 5], 
 g[1, 2] + g[2, 3] + g[3, 6], g[1, 2] + g[2, 4] + g[3, 7], 
 g[1, 2] + g[2, 4] + g[3, 8]}

*)
ciao
  • 25,774
  • 2
  • 58
  • 139
0

If anyone was interested in alternative (but not as elegant solution) this is the one I came up with recently (using lists as defined in the above solution).

Constant = 5;
lists = Table[G[i][[j]], {i, 1, Constant}, {j, 1, 2^i}];
Posi[i_, j_] := Ceiling[j/2]
model[1, j_] := lists[[1, j]];
model[i_, j_] := lists[[i, j]] + model[i - 1, Posi[i, j]];
finaltable = Table[model[i, j], {i, 1, Constant}, {j, 1, 2^i}];
Ibrahim
  • 169
  • 5