I have a list of the form,
mylist = {{0.1, 1.2},{0.3, 2.1},{0.1, 0.5},{0.4, 0.4},{0.4, 0.1},{0.1, 1.6}}
I would like to create a function, myfn, that takes a list and outputs a new list where the sublists with the same first element are combined, by adding the second elements together. For example in the above, I expect the output:
myfn[mylist]
(* Out: {{0.1, 3.3}, {0.3, 2.1}, {0.4, 0.5}} *)
I have the following piece of code that does the job, but it looks not particularly elegant and I was wondering if there are better ways to achieve this result?:
myfn[list_] := Module[{listnew = {list[[1]]}, pos},
Do[
Which[
Length@Position[First@Transpose[listnew], list[[i, 1]]] == 0,
listnew = Join[listnew, {list[[i]]}],
Length@Position[First@Transpose[listnew], list[[i, 1]]] == 1,
pos = Flatten@Position[listnew, list[[i, 1]]];
listnew[[pos[[1]], 2]] += list[[i, 2]],
True,
Print["Never get here in myfn. Aborting now..."]; Abort[]
]
, {i, 2, Length[list]}];
listnew
]