# Apply a Function Pairwise [duplicate]

I'm new to Mathematica

I'd like to apply Mean pairwise to a list to achieve the following.

badSource = {{0, 0}, {1, 1}, {2, 0}, {3, 1}, {4, 0}};
badInterpolation = {{.5, .5}, {1.5, .5}, {2.5, .5}, {3.5, .5}};


How can this be done in general? Do I need pure functions?

• Mean /@ Partition[N@badSource, 2, 1]
– BoLe
May 11, 2013 at 17:30
– BoLe
May 11, 2013 at 17:31
• Nice. There seems so be a specific solution to everything in Mathematica :-) May 11, 2013 at 17:38
• @BoLe Although the answers are short it's preferable that you provide them as answers, not comments. Otherwise, we'll have another seemingly unanswered question. May 11, 2013 at 17:38
• @BoLe, yup, those sure do look like answers to me. You might consider also including the ListConvolve[]/ListCorrelate[] version to cover all the bases. May 11, 2013 at 17:39

You need partitioning, Partition and parameters: 2 for pairs, 1 for unit overhang/offset, and then averaging each pair, using Map, short-notated /@.

Partition[{a, b, c, d}, 2, 1]

{{a, b}, {b, c}, {c, d}}


These will all make the averages:

Mean /@ Partition[N@badSource, 2, 1]


• Instead of the form f /@ Partition[list, ...] use DeveloperPartitionMap[f, list, ...]. May 11, 2013 at 20:04
• @chris I don't know. Intention wise, it is more apparent, and you can add Developer  to your \$ContextPath, so it is almost as simple to type. May 13, 2013 at 12:19
• @rcollyer Great, I didn't know for these additional functions. Help says PartitionMap[f, list, n] is equivalent to Map[f, Partition[list, n]]. Does this, being equivalent, in general means that two calls always return same result yet they don't need to be implemented in the same way?
• @rcollyer I assume that PartitionMap is not implemented in terms of Map and Partition, at least not in their normal syntax and behavior. The latter will do the full partitioning first, then the mapping. PartitionMap does the mapping while the partitioning is done. For example, RandomChoice /@ Partition[Range@1*^6, 100, 1]; will use ~800MB of memory (peak), whereas PartitionMap[RandomChoice, Range@1*^6, 100, 1]; will use ~23MB. May 15, 2013 at 11:45