Partial sum of a list
Accumulate@Last@Transpose[A]/Range@Length@A
{b, (b + d)/2, 1/3 (b + d + f), 1/4 (b + d + f + h)}
Clear[f,g, list];
list= {{a,b}, {c, d}, {e, f}, {g, h}}
f[list_,n_]:=Part[FoldList[Plus,Last@Transpose@list] / Range@Length@list, n]
or another version I personally prefer (sadly no operator forms for FoldList
and Part
)
g[list_, n_]:=list //Transpose
//Last
//FoldList[Plus, #]&
//Divide[#,Range@Length@list]&
//Extract[n]
I especially like that one can out-comment or partially copy the function definition to find out what the function does step by step
Edit: Instead of //Extract[n]
previously //Part[#,n]&
was used in the definition of g
. Without regard for a potential difference in performance I now like Extract[n]
better.
list = {{a, b}, {c, d}, {e, f}, {g, h}};
Mean[Take[Last /@ list, #]] & /@ Range@Length@list