Skip to main content

Apply a Function Pairwise



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}};
ListLinePlot[{badSource, badInterpolation}, Mesh -> All]

Pairwise Mean



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



Answer



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]


MovingAverage[N@badSource, 2]

ListConvolve[{{.5}, {.5}}, badSource]

ListCorrelate[{{.5}, {.5}}, badSource]

Last two suggested by J. M.


Comments