# Sectional averages of a list

I am working in Mathematica. I have a table of 200 elements. I want to calculate the mean value for the first 20 elements, for the second 20 elements and so on, up to 200. I will get a table (matrix) of 10 elements. How I can solve this problem? Thanks for helping.

• Your question is specific enough to have a proper title ;) And try Mean /@ Partition[data, 20] – Öskå Apr 14 '14 at 9:22
• I'd like to reopen this as I think it has some value but I don't want to be heavy handed. Does anyone agree? – Mr.Wizard May 30 '14 at 9:26
• @Mr.Wizard one agrees. But why not simply put it to the reopen vote? – Yves Klett May 30 '14 at 9:55
• @Yves Thanks. As a moderator I cannot cast a "normal" reopen vote; it will directly reopen the Question. – Mr.Wizard May 30 '14 at 10:07
• @Mr.Wizard - since your blockAverage2 is significantly faster than all other alternatives I tried it MUST be reopened! – eldo May 30 '14 at 10:08

As Öskå notes you can Partition your data and then Map Mean:

a = {q, r, s, t, u, v, w, x, y};

Mean /@ Partition[a, 3]

{1/3 (q + r + s), 1/3 (t + u + v), 1/3 (w + x + y)}


However if performance is a concern I propose using Total or Dot:

blockAverage1[a_List, n_Integer] := a ~Partition~ n ~Total~ {2} / n
blockAverage2[a_List, n_Integer] := Partition[a, n].ConstantArray[1/n, n]


Timings:

a = RandomReal[9, 5*^7]; (* big list *)

Mean /@ Partition[a, 20] // Timing // First
blockAverage1[a, 20]     // Timing // First
blockAverage2[a, 20]     // Timing // First

1.311

0.0654

0.0306


Related:

In versions 10.2+ there is BlockMap:

a = {q, r, s, t, u, v, w, x, y};

BlockMap[Mean, a, 3]


{1/3 (q + r + s), 1/3 (t + u + v), 1/3 (w + x + y)}

Although this is much slower than the alternatives in Mr.Wizard's answer, its elegance may be of value since OP says

I have a table of 200 elements

Also, an undocumented 6-argument form of Partition:

Partition[a, 3, 3, None, {}, Mean[{##}] &]


{1/3 (q + r + s), 1/3 (t + u + v), 1/3 (w + x + y)}