I have a simple list mylist={x_1,x_2,...,x_N};
.
Now I want to map a function over this list, e.g. the Mean and StandardDeviation functions, so that it will give me a list that looks like this:
f/@mylist = {f[{x_1}], f[{x_1, x_2}], f[{x_1, x_2, x_3}], ... , f[{x_1,x_2,...,x_N}]}
How can I achieve that? I know that I can use a simple loop to do so but I am wondering if Mathematica has a specific function for this kind of mapping.
Thanks a bunch!
Table
be an acceptable solution or would that count as a simple loop? While Mathematica hasAccumulate
which performs the operation you look for forPlus
, I don't think there's a way to do a generalised accumulation with an arbitrary function likeMean
. Neither is there a built-in to get all the prefixes of a list, so I guess usingTable
orArray
might be the best you can get. (E.g.Table[Mean@myList[[1 ;; i]], {i, Length@myList}]
) $\endgroup$f@Take[mylist, #] & /@ Range[4]
orMean@Take[mylist, #] & /@ Range[4]
, maybe $\endgroup$ClearAll[MapAccumulate]; MapAccumulate[f_, list_List] := f@Take[list, #] & /@ Range@Length@list;
...MapAccumulate[f, mylist]
$\endgroup$