Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

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?

share|improve this question

marked as duplicate by Mr.Wizard Jan 23 '15 at 22:41

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Mean /@ Partition[N@badSource, 2, 1] – BoLe May 11 '13 at 17:30
MovingAverage[N@badSource, 2] – BoLe May 11 '13 at 17:31
Nice. There seems so be a specific solution to everything in Mathematica :-) – schmijos May 11 '13 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. – Sjoerd C. de Vries May 11 '13 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. – J. M. May 11 '13 at 17:39
up vote 13 down vote accepted

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.

share|improve this answer
Instead of the form f /@ Partition[list, ...] use Developer`PartitionMap[f, list, ...]. – rcollyer May 11 '13 at 20:04
@rcollyer why? Is it faster? – chris May 13 '13 at 7:58
@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. – rcollyer May 13 '13 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? – BoLe May 13 '13 at 17:03
@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. – Mr.Wizard May 15 '13 at 11:45

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