Message Boards Message Boards

0
|
3250 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Combining several lists

Posted 11 years ago

Here is a question for more elegant programming:

pj = {j - 1, j, j + 1}

pi = {i - 1, i, i + 1}

pk = {k - 1, k, k + 1}

I want to combine these three lists which have three elements each, in such a way that each element of one list is combined with one element of all remaining lists. When I use Partition, Flatten and Table Commands, the result looks like

pt = Partition[
  Flatten[Table[{i + (u1 - 2), j + (u2 - 2), k + (u3 - 2)}, {u1, 1, 
     3}, {u2, 1, 3}, {u3, 1, 3}]], 3]

{{-1 + i, -1 + j, -1 + k}, {-1 + i, -1 + j, k}, {-1 + i, -1 + j, 
  1 + k}, {-1 + i, j, -1 + k}, {-1 + i, j, k}, {-1 + i, j, 
  1 + k}, {-1 + i, 1 + j, -1 + k}, {-1 + i, 1 + j, k}, {-1 + i, 1 + j,
   1 + k}, {i, -1 + j, -1 + k}, {i, -1 + j, k}, {i, -1 + j, 
  1 + k}, {i, j, -1 + k}, {i, j, k}, {i, j, 1 + k}, {i, 
  1 + j, -1 + k}, {i, 1 + j, k}, {i, 1 + j, 
  1 + k}, {1 + i, -1 + j, -1 + k}, {1 + i, -1 + j, k}, {1 + i, -1 + j,
   1 + k}, {1 + i, j, -1 + k}, {1 + i, j, k}, {1 + i, j, 
  1 + k}, {1 + i, 1 + j, -1 + k}, {1 + i, 1 + j, k}, {1 + i, 1 + j, 
  1 + k}}

Length[pt]
27

Now, I want to combine n lists (p1, p2, ..., pn), which have three elements each in the way described above. The resulting list is a {3^n, n} matrix. I am looking for an elegant solution.

Best,

GG

POSTED BY: Gilbert Gosseyn
2 Replies
Posted 11 years ago

Dear Bill,

Thanks. Now, I have turned it into:

Flatten[Outer[List, ##] & @@ pp, n - 1],

where pp = {pi, pj, pk}

Best,

GG

POSTED BY: Gilbert Gosseyn
Posted 11 years ago

.Hint

Flatten[Outer[List,pi,pj,pk],2]
POSTED BY: Bill Simpson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract