0
$\begingroup$

I have a function r, which removes the first instance of an element of a list

r[m_, el_] := Delete[m, First@First@Position[m, el]]

I also have nested tables and n is the number of nested tables, so in this example n=4

Table[
  Table[
    Table[
      Table[
        {i, j, k, l, Total@T - i - j - k - l},
        {i, r[r[r[T, l], k], j]}
      ], {j, r[r[T, l], k]}
    ], {k, r[T, l]}
  ], {l, T}
]

And I want n to be variable, so if n=2, the nested tables should be

Table[
  Table[
    {i, j, Total@T - i - j},
    {i, r[T, j]}
  ], {j, T}
]

The goal is to have a function that takes n and T as arguments and outputs this nested table.

Note that in each argument of each Table the range is almost an element of NestList[r[#,l]&,T,n], but it's not exactly that. Because l in this example is different in each iteration.

EDIT:

Imagine that to do a task you need to do n subtasks, and each subtask needs some time to be done. This is T. When finishing a subtask t_i you need some time to get to the start of another subtask and I want to find the order in which you need to do the tasks to make it the most efficient.

$\endgroup$
  • $\begingroup$ This looks somewhat complicated; perhaps a simpler solution might exist, but could you show an example of your desired final output for some specific values of n and T? $\endgroup$ – MarcoB Feb 17 '17 at 18:39
  • 3
    $\begingroup$ Very inelegant coding, which doesn't exploit the natural efficiencies and functions of Mathematica. $\endgroup$ – David G. Stork Feb 17 '17 at 18:42
  • $\begingroup$ Considering your last statement about NestList, then perhaps FoldList could work for you, if you use a list of the values of l that are of interest. $\endgroup$ – MarcoB Feb 17 '17 at 18:43
  • $\begingroup$ @MarcoB Edited the question accordingly. $\endgroup$ – Garmekain Feb 17 '17 at 18:57
  • 1
    $\begingroup$ @Garmekain Thank you for the edit, but could you state the problem more clearly? I don't immediately see the relationship between the values in T and the elements of the matrix to be considered. A really good idea would be to take an actual matrix, with some random numbers in it (say for instance SeedRandom[1]; RandomInteger[{1, 100}, {4, 4}]), and actual value of T, such as perhaps the one you used above, and show us the actual output you seek. I am almost positive that the nested Table can be improved upon, but I'm afraid that I can't help if I don't understand what you want to do. $\endgroup$ – MarcoB Feb 17 '17 at 19:38
2
$\begingroup$

There may be better ways to do this, but here is how literally to build up your nested table for variable n :

r[m_, el_?NumericQ] := Delete[m, First@First@Position[m, el]]

Note the function r has to be fixed to only evaluate for numeric arguments.

Your original table for n=4:

T = {1, 2, 3, 2, 1}
a = Table[
  Table[Table[
    Table[{i, j, k, l, Total@T - i - j - k - l},
      {i, r[r[r[T, l], k], j]}], {j, r[r[T, l], k]}],
      {k, r[T, l]}], {l,T}];

first write as a single nested Table. Note the iterator ordering is outside to inside left to right:

Table[{i, j, k, l, Total@T - i - j - k - l}, {l, T}, {k, r[T, l]}, {j,
    r[r[T, l], k]}, {i, r[r[r[T, l], k], j]}] == a

True

then construct the lists for variable n:

n = 4
iters = Table[Unique[], {n}];
itlist = Transpose[{Reverse@iters, FoldList[r, T, Reverse@Rest@iters]}];
expr = Append[iters, Total@T - Total@iters];
Table[expr, Evaluate[Sequence @@ itlist]] == a

True

$\endgroup$
  • $\begingroup$ btw, I think r[m_, el_?NumericQ] := Delete[m, First@First@Position[m, el]] can be simplified to r[m_, el_?NumericQ] := Delete[m, First@Position[m, el]] i.e. remove one of those First ;) $\endgroup$ – Nasser Feb 17 '17 at 19:56
  • $\begingroup$ yes, Delete takes an integer or a list with one integer the same. $\endgroup$ – george2079 Feb 17 '17 at 20:01
  • $\begingroup$ @george2079 My bad, I understood that I needed to put Evaluates to use iters as part specifications. $\endgroup$ – Garmekain Feb 17 '17 at 20:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.