1

I currently use MapThread[f, {{a1, a2}, {b1, b2}] for obtaining { f[a1, b1], f[a2, b2] }.

Is there a version (or alternative) for obtaining { f[a1, b1, c], f[a2, b2, c] }?

Basically I'd like to add a fixed option c (like c1 -> c11) to each invocation of f. How can it be done?

Drux
  • 285
  • 1
  • 6

3 Answers3

5

Since c is common to all the entries, I would do this:

MapThread[f[##, c] &, {{a1, a2}, {b1, b2}}]

(* ==> {f[a1, b1, c] , f[a2, b2, c]} *)

Here the ## stands for SlotSequence and accepts the pair of arguments fed into it by Inner, taken from the two Lists.

This is based on constructing a pure function (identified by the & at the end) that is used instead of f.

This also works in the special case that c is a rule:

MapThread[f[##, c1 -> c11] &, {{a1, a2}, {b1, b2}}]

(* ==> {f[a1, b1, c1 -> c11], f[a2, b2, c1 -> c11]} *)
Jens
  • 97,245
  • 7
  • 213
  • 499
3
Thread[f[{a1, a2}, {b1, b2}, c]]
(* {f[a1, b1, c], f[a2, b2, c]} *)

Thread[f[{a1, a2}, {b1, b2}, c1 -> c11]]
(* {f[a1, b1, c1 -> c11], f[a2, b2, c1 -> c11]} *)
kglr
  • 394,356
  • 18
  • 477
  • 896
  • (+1) for the simplest approach. – Jens Mar 12 '15 at 17:37
  • If you try this with HighlightImage as f it will complain about expecting an image instead of a list of images. – Drux Mar 12 '15 at 17:37
  • @Drux - yes, the dimensions of the lists on which you work is important for Thread. It doesn't matter in your original example. But MapThread then would offer simpler control over the level at which the threading happens. It can be done with Thread, too, though. – Jens Mar 12 '15 at 17:39
  • Thank you @Jens. Drux, try Unevaluated@HighlightImage instead of HighlightImage. – kglr Mar 12 '15 at 17:50
  • @Drux maybe try Thread[f[{a1, a2}, {b1, b2}, c1 -> c11]] /. f-> HighlightImage where f is a "dummy" symbol without any definition – Mike Honeychurch Mar 12 '15 at 22:55
0
f[##, c] & @@@ (Transpose[{{a1, a2}, {b1, b2}}])
    (*{f[a1, b1, c], f[a2, b2, c]}*)
Basheer Algohi
  • 19,917
  • 1
  • 31
  • 78