Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Topic: Average the same elements of the list
Replies: 4   Last Post: May 24, 2013 5:24 AM

 Messages: [ Previous | Next ]
 Murray Eisenberg Posts: 2,105 Registered: 12/6/04
Re: Average the same elements of the list
Posted: May 23, 2013 3:55 AM

You didn't specify what to return in case the first elements of the
sublists are NOT all the same; let's return the entire original list in
this case. Then perhaps something like the following will do.

op[lis_] :=
If[And @@ MapThread[Equal, First /@ lis],
{First@First@lis, Mean[Last /@ lis]},
lis]

op[{ {{2, 5, -1}, d1}, {{2, 5, -1}, d2}}]
(* { {2, 5, -1}, (d1 + d2)/2 } *)

op[{{{2, 5, -1}, d1}, {{2, 9, -1}, d2}}]
(* {{{2, 5, -1}, d1}, {{2, 9, -1}, d2}} *)

Undoubtedly the code can be shortened.

On May 22, 2013, at 2:18 AM, BBabic <bipsich101@gmail.com> wrote:

> Hello,
> I have list which is something like
> data={
> {{a1,b1,c1},d1},{{a2,b2,c2},d2}}
> I would like to get new list which gets average of the second elements if the first elements in the sublists are all the same.
> Namely if a1=a2,b1=b2,c1=c2
> new list would look like
> datanew={{a1,b1,c1},Mean[{d1,d2}]
> Is there an elegant way to do this ?
> Thanks!

---
Murray Eisenberg murray@math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower phone 413 549-1020 (H)
University of Massachusetts 413 545-2838 (W)
710 North Pleasant Street fax 413 545-1801
Amherst, MA 01003-9305

Date Subject Author
5/23/13 Bob Hanlon
5/23/13 Tomas Garza Hernandez
5/23/13 Murray Eisenberg
5/24/13 Daniel