# MovingAverage to include the average of the first and last elements in a list

Is there any way to take the average of pairwise elements in a list?

Example: Given the list

list = {a,b,c}


I'd like to generate the list

{(a+b)/2, (b+c)/2, (a+c)/2}


In this case, MovingAverage almost does the job with the exception of averaging a and c. Adding a at the end of list does the job, but what if I absolutely have to preserve the contents of list?

## 4 Answers

list = {a, b, c};
Append[MovingAverage[list, 2], Mean[{First[list], Last[list]}]]

{(a+b)/2, (b+c)/2, (a+c)/2}


You can define a function if you want:

newMovingAverage[list_] := Append[MovingAverage[list, 2], Mean[{First[list], Last[list]}]]

newMovingAverage[list]

{(a+b)/2, (b+c)/2, (a+c)/2}

• Ah nice, that was fast! Thanks to both answers! May 27, 2015 at 3:01
• @user170231 Glad I could help! Thank you for the accept.
– Ivan
May 27, 2015 at 3:25
list = {a, b, c};

Mean[{#, RotateLeft@#}]& @ list
Mean /@ Partition[#, 2, 1 , 1]& @ list
DeveloperPartitionMap[Mean, #, 2, 1, 1]& @ list
MovingAverage[ArrayPad[#, {0, 1}, "Periodic"], 2]& @ list
MovingAverage[PadRight[#, 1 + Length@#, #], 2]& @list
({##} + { ##2, #})/2 & @@ list

{(a + b)/2, (b + c)/2, (a + c)/2}


### Perfomance Comparison

sol1 = Mean[{#, RotateLeft@#}] &;
sol2 = Mean /@ Partition[#, 2, 1, 1] &;
sol3 = DeveloperPartitionMap[Mean, #, 2, 1, 1] &;
sol4 = MovingAverage[ArrayPad[#, {0, 1}, "Periodic"], 2] &;
sol5 = MovingAverage[PadRight[#, 1 + Length@#, #], 2] &;
sol6 = .5 ({##} + {##2, #}) & @@ # &;
RunnyKine = .5 ListCorrelate[{1, 1}, #, 1] &;
Ivan = Append[MovingAverage[#, 2], Mean[{First[#], Last[#]}]] &;

timing[func_, n_] :=
First@(AbsoluteTiming[func[RandomReal[{1, 10}, 10^n]]])


Test

Table[timing[func, #] & /@
Range[7], {func, {sol1, sol2, sol3, sol4, sol5, sol6, RunnyKine,Ivan}}];

ListLinePlot[%, PlotRange -> {{0, 7}, {0, 12}},
AxesLabel -> {"n", "time"},
PlotLegends -> {"sol1", "sol2", "sol3", "sol4", "sol5", "sol6",
"RunnyKine","Ivan"}
]


One can also use ListCorrelate or ListConvolve which I expect to be quick:

1/2 ListCorrelate[{1, 1}, list, 1]


Gives:

{(a + b)/2, (b + c)/2, (a + c)/2}

• That's the way to do it if speed is needed....+1
– ciao
May 27, 2015 at 6:11
• Why not have the kernel be {1, 1}/2 to begin with? May 27, 2015 at 8:28
• @J. M. That was my initial kernel, I tried the current one for speed gains but noticed there weren't any. I just decided this one looked cleaner to me. May 27, 2015 at 13:31

This seems pretty speedy:

With[{l = Divide[#, 2]}, Append[Most@l + Rest@l, Plus @@ l[[{1, -1}]]]] &

• Nicely done and quite speedy. May 27, 2015 at 8:13