2

I have a function whose arguments have the form F[a,b,c,d,e], but the 2nd to 4th arguments are in a list of the form R={{{b,c,d},{e,f,g}},{{h,i,j},{k,l,m}}} how can I pass these elements as those the arguments of the function.

Here I have found a similar question, but in that case, all the arguments are in a list.

codebpr
  • 2,233
  • 1
  • 7
  • 26
Alex97
  • 410
  • 2
  • 16
  • R has 2 items per sublist and there are 2 items. I was just wondering if these were four lists of 3 elements each? – Syed Nov 28 '21 at 08:11
  • R is a list that contains two list that each of them has 2 list of 3 elements. Sorry...I edited the title. @Syed – Alex97 Nov 28 '21 at 08:29
  • Oh i see. I thought you wanted to pass a 2D matrix into the function and that didn't make sense either. You can use F[p,q,r,s,t] to make the post more clear as the other symbols have been used in R. – Syed Nov 28 '21 at 08:35

4 Answers4

2
R = {{{b, c, d}, {e, f, g}}, {{h, i, j}, {k, l, m}}};

Apply[F[a, ##] &, R, {2}]
{{F[a, b, c, d], F[a, e, f, g]}, {F[a, h, i, j], F[a, k, l, m]}}

Also

Map[F[a, ##] & @@@ # &] @ R
{{F[a, b, c, d], F[a, e, f, g]}, {F[a, h, i, j], F[a, k, l, m]}}

and

R /. {x_, y_, z_} :> F[a, x, y, z]
{{F[a, b, c, d], F[a, e, f, g]}, {F[a, h, i, j], F[a, k, l, m]}}
kglr
  • 394,356
  • 18
  • 477
  • 896
2
R = {{{b, c, d}, {e, f, g}}, {{h, i, j}, {k, l, m}}};

Using Replace at level 2:

Replace[R, v_ :> F[a, Sequence @@ v], {2}]

({{F[a, b, c, d], F[a, e, f, g]}, {F[a, h, i, j], F[a, k, l, m]}})

E. Chan-López
  • 23,117
  • 3
  • 21
  • 44
1
R = {{{b, c, d}, {e, f, g}}, {{h, i, j}, {k, l, m}}};

Using MapApply (new in 13.1)

MapApply[F] /@ Map[Prepend[a], R, {2}]

{{F[a, b, c, d], F[a, e, f, g]}, {F[a, h, i, j], F[a, k, l, m]}}

A variant of kglr's ReplaceAll

R /. {x__?AtomQ} :> F[a, x]

{{F[a, b, c, d], F[a, e, f, g]}, {F[a, h, i, j], F[a, k, l, m]}}

eldo
  • 67,911
  • 5
  • 60
  • 168
1
R = {{{b, c, d}, {e, f, g}}, {{h, i, j}, {k, l, m}}};

R /. v_?VectorQ :> F[a, Sequence @@ v]

{{F[a, b, c, d], F[a, e, f, g]}, {F[a, h, i, j], F[a, k, l, m]}}

Syed
  • 52,495
  • 4
  • 30
  • 85