1
$\begingroup$

I have a table of parameter pairs a and b {{a1, b1}, {a2, b2}, ...} and I would like to evaluate a function with these parameter pairs. What I tried so far:

Table[a + b, {a, {1, 2}}, {b, {3, 4}}]

As a result I do get:

{{4, 5}, {5, 6}}

So Mathematica evaluated the function a + b with all possible combinations of the list of a and b. However, I would like to have a table with the evaluation of the fuction 1st with a = 1 and b = 3 and then with a = 2 and b = 4 and not the other combinations. How do I realize this?

$\endgroup$

closed as off-topic by m_goldberg, b3m2a1, Henrik Schumacher, MarcoB, Sektor Jan 31 '18 at 7:35

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – m_goldberg, b3m2a1, Henrik Schumacher, MarcoB, Sektor
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ MapThread[foo, {{1, 2}, {3, 4}}] or Inner[foo, {1, 2}, {3, 4}, List] or foo @@@ Transpose[{{1, 2}, {3, 4}}] or Thread[foo[{1, 2}, {3, 4}]] ?? $\endgroup$ – kglr Jan 23 '18 at 16:11
  • $\begingroup$ ... use Plus for foo to get your a+b example. $\endgroup$ – kglr Jan 23 '18 at 16:12
  • $\begingroup$ f @@@ {{1, 3}, {2, 4}}? $\endgroup$ – Michael E2 Jan 23 '18 at 16:25
2
$\begingroup$

Apply[f, {ab1, ab2,...}, {1}] or f @@@ {ab1, ab2,...}:

Plus @@@ {{1, 3}, {2, 4}}
(* {4, 6} *)

Plus @@@ {{1, 3}, {2, 4}, {3, 5}}
(* {4, 6, 8} *)
$\endgroup$
  • $\begingroup$ Thank you for your help! $\endgroup$ – JPK Jan 24 '18 at 9:45
  • $\begingroup$ @JPK You're welcome. :) $\endgroup$ – Michael E2 Jan 24 '18 at 12:07
1
$\begingroup$
Thread[foo[{a, b}, {c, d}]]
foo @@@ Thread[{{a, b}, {c, d}}]
foo @@@ Transpose[{{a, b}, {c, d}}]
Inner[foo, {a, b}, {c, d}, List]
MapThread[foo, {{a, b}, {c, d}}]

{foo[a, c], foo[b, d]}

For Plus you can simply use (thanks: MichaelE2)

{a, b} + {c, d} (* or *)
Plus[{a, b}, {c, d}] 

to get

{a + c, b + d}

Actually, the same is true for all Listable functions such as Times and Power:

Times[{a, b, c}, {x, y, z}]

{a x, b y, c z}

{a,b,c}^{x,y,z}

{a^x, b^y, c^z}

$\endgroup$
  • $\begingroup$ Thank you for your help! $\endgroup$ – JPK Jan 24 '18 at 9:45
  • $\begingroup$ @JPK, my pleasure. $\endgroup$ – kglr Jan 25 '18 at 20:43

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