The following code is correct but is highly inefficient:
data2 = RandomVariate[NormalDistribution[0, 1], 10^6]; MyList2 = Table[Mean[Take[data2, i]], {i, Length[data2]}];
Is there a concise way of doing this more efficiently?
Thank you all for the helpful responses.
Look at this
In[119]:= FoldList[Plus, a, {b, c, d, f}]/Range[5] Out[119]= {a, (a + b)/2, 1/3 (a + b + c), 1/4 (a + b + c + d), 1/5 (a + b + c + d + f)}
with other words
FoldList[Plus, First[data2], Rest[data2]]/Range[Length[data2]]
should do it.
Somewhat faster, by keeping the arrays packed:
myList = Accumulate[data2]/N[Range[Length[data2]]]; myList == MyList2
takes about 0.01 seconds on my machine.
If you're wanting to calculate the sequential means of observations 1, 1+2, 1+2+3, etc., then
MyEfficientList = Accumulate[data2]/Table[i, {i, Length[data2]}]
is much faster.
If you're then going to do something with those sequential means, there might be more efficient ways to get to that final product.