# 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, 2014 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? May 30, 2014 at 9:26
• @Mr.Wizard one agrees. But why not simply put it to the reopen vote? May 30, 2014 at 9:55
• @Yves Thanks. As a moderator I cannot cast a "normal" reopen vote; it will directly reopen the Question. May 30, 2014 at 10:07
• @Mr.Wizard - since your blockAverage2 is significantly faster than all other alternatives I tried it MUST be reopened!
– eldo
May 30, 2014 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)}

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


Using SequenceCases (new in 10.1)

SequenceCases[list, x : {_, _, _} :> Mean @ x]


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

Generalization

SequenceCases[list, x : {Repeated[_, {2}]} :> Mean @ x]


{(q + r)/2, (s + t)/2, (u + v)/2, (w + x)/2}

Using SubsetCases:

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

SubsetCases[list, s : {_, _, _} :> Mean@s]

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


Or using SequenceSplit:

SequenceSplit[list, s : {_, _, _} :> Mean@s]

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