5
$\begingroup$

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.

$\endgroup$
2
  • 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$ Commented Jun 8, 2017 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$ Commented Jun 9, 2017 at 1:12

5 Answers 5

4
$\begingroup$

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]}}
$\endgroup$
0
6
$\begingroup$
Function[f, f @@ x, Listable] @ F
$\endgroup$
3
$\begingroup$

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]}}
$\endgroup$
2
$\begingroup$
f = {{f11, f21}, {f12, f22}};

x = {x1, x2, x3, x4, x5};

Using ComapApply (new in 14.0)

ComapApply[#, x] & /@ f

gives

{{f11[x1, x2, x3, x4, x5], f21[x1, x2, x3, x4, x5]},
 {f12[x1, x2, x3, x4, x5], f22[x1, x2, x3, x4, x5]}}

With Query

Query[Apply /@ #] @ x & /@ f

Same result

$\endgroup$
1
$\begingroup$

An alternative using Table:

F = {{f11, f21}, {f12, f22}};
x = {x1, x2, x3, x4, x5};

Table[F[[i, j]] @@ x, {i, #}, {j, #}] &@Length@F

(*{{f11[x1, x2, x3, x4, x5], f21[x1, x2, x3, x4, x5]}, 
   {f12[x1, x2, x3, x4, x5], f22[x1, x2, x3, x4, x5]}}*)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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