4
$\begingroup$

I have a list of lists of fitted parameter values, e.g.

list={{a->1,b->10},{a->2,b->20},{a->3,b->30}}

(the actual parameter names can vary, as well as the size of the list)

What is the easiest way then to obtain the average parameter values, in this case avg={a->2,b->20} ?

$\endgroup$
1
  • $\begingroup$ Also: (4332) $\endgroup$
    – Mr.Wizard
    Commented Feb 16, 2016 at 14:10

2 Answers 2

4
$\begingroup$
Normal@Merge[Mean][list]
(* {a -> 2, b -> 20} *)
$\endgroup$
4
  • $\begingroup$ Perfect - thanks a lot!! $\endgroup$ Commented May 1, 2015 at 9:31
  • 1
    $\begingroup$ This is nifty: is it efficient? What if the list is very very long? Does going through Association have a penalty? $\endgroup$
    – chris
    Commented May 1, 2015 at 9:58
  • $\begingroup$ @Tom, my pleasure. Thank you for the accept. $\endgroup$
    – kglr
    Commented May 1, 2015 at 13:20
  • $\begingroup$ @chris, good question. I am not sure about the relative efficiency. It would be a good idea to do timing comparisons after a few more alternatives come in. $\endgroup$
    – kglr
    Commented May 1, 2015 at 13:27
1
$\begingroup$

A method that works on older versions of Mathematica is this:

{a, b} -> Map[Mean, Transpose[{a, b} /. list]]

but admittedly it won't be as fast as Merge.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.