1
$\begingroup$

I'm stuck with a List and Table command. I want to evaluate $F[x,y]$ and get all the possible configurations, knowing that x and y are lists. How I should evaluate this correctly in Mathematica? and thanks in advance!

F[x_, y_] :=Flatten[Table[{a, b, c, d}, {a, 1, 2}, {b, 0, x - a + 1}, {c, 1, 2}, {d, 1, y - 1}], 3]; x = {14, 9, 7, 5, 10}; y = {1, 3, 2, 4, 5};

$\endgroup$
2
  • $\begingroup$ If you want all possible combinations of list elements what is typically useful is Outer, e.g.: Flatten[Outer[List, {1, 2}, x, {1, 2}, y], 3]. Does this give the output you want? $\endgroup$ – Andrzej Jun 7 at 18:46
  • $\begingroup$ Andrzej, I think Flatten[Outer[F, x, y], 2] is another good answer to the problem. Why don't you add it? $\endgroup$ – Jack LaVigne Jun 8 at 15:01
1
$\begingroup$

I am assuming that you want integers as the input to the function F[x_,y_] and that you want to apply them to all combinations of the lists, x and y.

Continue on with your use of Table and apply it to x and y. Table is very powerful and can accept lists in addition to iteration parameters.

Table[F[x, y], {x, {14, 9, 7, 5, 10}}, {y, {1, 3, 2, 4, 5}}]
$\endgroup$
3
  • $\begingroup$ Thanks, it's work but i want all the results look like { {1, 0, 1, 1}, {1, 0, 1, 2}, {1, 0, 2, 1}, {1, 0, 2, 2},........ } bcz i get sum of lists inside a lists{{{}, {{1, 0, 1, 1}, {1, 0, 1, 2}, {1, 0, 2, 1}, {1, 0, 2, 2}, {1, 1, 1, 1}........}}}} $\endgroup$ – Yduog chan Jun 7 at 18:14
  • $\begingroup$ Wrap the result with another Flatten. Flatten accepts a third argument indicating how many times it should be applied. Flatten[Table[F[x, y], {x, {14, 9}}, {y, {3}}], 2] $\endgroup$ – Jack LaVigne Jun 7 at 19:53
  • $\begingroup$ Thanks a lot! for the help. $\endgroup$ – Yduog chan Jun 8 at 9:43

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