1

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?

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
JPK
  • 21
  • 2

2 Answers2

2

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} *)
Michael E2
  • 235,386
  • 17
  • 334
  • 747
1
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}

kglr
  • 394,356
  • 18
  • 477
  • 896