Skip to main content

Merging lists of pairs



I want to add several fluorescence spectra. They are lists of the format


spectrum 1 = {{x1,y11},{x2,y12},{x3,y13},...}

spectrum 2 = {{x1,y21},{x2,y22},{x3,y23},...}

I want to create a list with the original x-values, but the y-value being the y-value of list 1 + y-value of list 2 + ..., so in code:


result = {{x1,y11+y21},{x2,y12+y22},{x3,y13+y23},...}

Please notice that I will have approx. 50 lists that have to be added, not two.


Additional problem: Some of the lists have different length since they comprise a different wavelength-range. For example a list that doesn't start with x1:


list 7 = {{x4, y71},{x5,y72},{x6,y73},...}

Thanks.




Answer



v10 only and using @kglr's spectra...


List @@@ Normal@
GroupBy[Join[spectrum1, spectrum2, spectrum3], First -> Last, Total]


{{x1, y11 + y21}, {x2, y12 + y22}, {x3, y13 + y23 + y33}, {x4, y14 + y24 + y34}, {x5, y15 + y25 + y35}}



Comments