I have

F = {{f11,f21},{f12,f22}}; (*Could be larger*)
x = {x1,x2,x3,x4,x5}; (*Could be larger*)

I want to have a matrix Fx that is equal to matrix F, where every entry fij is

fij = fij[x1,x2,x3,x4,x5]

I tried with Apply and Map but I can't get it right.

  • 3
    $\begingroup$ There are multiple ways to do it -- here is one with Apply and Map: Map[#1 @@ x &, F, {-1}] . (BTW, I voted to close.) $\endgroup$ – Anton Antonov Jun 8 '17 at 19:32
  • 1
    $\begingroup$ Avoid using user variables, which start with a capital letter. E.g. avoid using F as variable name. Use f instead. Alternatively, if you absolutely have to use a capital F, use [ScriptCapitalF] or [DoubleStruckCapitalF] (look very pretty in MMA). Type them in by typing esc+scF+esc or esc+dsF+esc. $\endgroup$ – Gregory Klopper Jun 9 '17 at 1:12

You may find Through command useful:

Through /@ Through[F @@ x]
{{f11[x1, x2, x3, x4, x5],   f21[x1, x2, x3, x4, x5]}, 
 {f12[x1, x2, x3, x4, x5],   f22[x1, x2, x3, x4, x5]}}
| improve this answer | |
Function[f, f @@ x, Listable] @ F
| improve this answer | |

So we can conduct testing with matrices and vectors of various sizes, let's write some code that makes it convenient to generate data,

xvec = 
  With[{n = 4}, 
    Array[x, n] /. x[i_Integer] :> Symbol[StringJoin["x", ToString[i]]]]

{x1, x2, x3, x4}

farr =
  With[{r = 2, c = 3}, 
    Array[f, {r, c}] /. 
     f[i_Integer, j_Integer] :> Symbol[StringJoin["f", ToString[i], ToString[j]]]]

{{f11, f12, f13}, {f21, f22, f23}}

Now we map the pure function # @@ xvec & over the lowest level of farr.

Map[# @@ xvec &, farr, {-1}]
{{f11[x1, x2, x3, x4], f12[x1, x2, x3, x4], f13[x1, x2, x3, x4]}, 
 {f21[x1, x2, x3, x4], f22[x1, x2, x3, x4], f23[x1, x2, x3, x4]}}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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