Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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 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
3  
MovingAverage[N@badSource, 2] –  BoLe May 11 '13 at 17:31
2  
Nice. There seems so be a specific solution to everything in Mathematica :-) –  Josua Schmid 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

1 Answer 1

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
1  
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
3  
@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.