1. Problem statement
In accordance with the standard definitions the inbuilt MovingAverage
list = {a, b, c, d, e};
n = 3;
MovingAverage[list, n]
{(a + b + c)/3, (b + c + d)/3, (c + d + e)/3}
shortens the original list. But in some cases (f.e. if the values are very similar) it might be desirable to avoid this shortening.
I thought there should be an inbuilt function (variant) which would do this. So I looked at MovingMap
, TimeSeries
and related functions, but I didn't find one.
2. Current solution
After some struggling with the Partition
parameters I found the following solution:
par = Partition[list, n, 1, -1, {}]
{{a}, {a, b}, {a, b, c}, {b, c, d}, {c, d, e}}
MapApply[Plus] @ par / Map[Length, par]
{a, (a + b)/2, (a + b + c)/3, (b + c + d)/3, (c + d + e)/3}
3. Other examples
list = {a, b, c, d, e, f};
n = 4;
Plus @@@ # / Map[Length, #] & [Partition[list, n, 1, -1, {}]]
{a, (a + b)/2, (a + b + c)/3, (a + b + c + d)/4, (b + c + d + e)/4, (c + d + e + f)/4}
list = {a, b};
n = 2;
Plus @@@ # / Map[Length, #] & [Partition[list, n, 1, -1, {}]]
{a, (a + b)/2}
4. Question
I'm curious to see what alternative solutions would look like
par
, wouldn'tMean /@ par
achieve the result you seek? $\endgroup$