Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #14597 > unrolled thread
| Started by | Bob Hanlon <hanlonr357@gmail.com> |
|---|---|
| First post | 2013-05-23 07:55 +0000 |
| Last post | 2013-05-23 07:55 +0000 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.soft-sys.math.mathematica
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Average the same elements of the list Bob Hanlon <hanlonr357@gmail.com> - 2013-05-23 07:55 +0000
| From | Bob Hanlon <hanlonr357@gmail.com> |
|---|---|
| Date | 2013-05-23 07:55 +0000 |
| Subject | Re: Average the same elements of the list |
| Message-ID | <knki2f$ier$1@smc.vnet.net> |
data = {{{a1, b1, c1}, d1}, {{a2, b2, c2}, d2},
{{a1, b1, c1}, d3}, {{a2, b2, c2}, d4},
{{a1, b1, c1}, d5}, {{a3, b3, c3}, d7}};
For v7 and later
sol = Plus @@ #/Length[#] & /@
GatherBy[data, First]
{{{a1, b1, c1}, (1/3)*(d1 + d3 + d5)},
{{a2, b2, c2}, (d2 + d4)/2}, {{a3, b3, c3}, d7}}
or (probably faster for longer lists)
sol == ({#[[1, 1]], Mean[#[[All, 2]]]} & /@
GatherBy[data, First])
True
For v3 or later
sol == (Plus @@ #/Length[#] & /@
Split[data // Sort, #1[[1]] == #2[[1]] &])
True
Bob Hanlon
On Wed, 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!
>
>
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web