I have two unequal-length lists of ordered pairs and I want to subtract the y-values one (the bigger one) from the other (the smaller one) if they have the same x value. Ideally, I should end up with a list that is the same size as the smaller one. A simple version of this might be that I'm given
big = {{0,1},{1,10},{2,5},{3,8},{4,9},{5,2}}
small = {{0,-1},{2,1},{5,10}}
And I want to do something such that I get the result
diff = {{0,-2},{2,-4},{5,8}}
So far, from a hodgepodge of other StackExchange answers, I've been trying
f = Composition[Apply[{First@#, Total@#2} &, #] &, Transpose] /@ GatherBy[Join[##], First] &;
big[[All,2]] = big[[All,2]]*(-1)
diff = f[small, big];
diff = DeleteCases[diff, Alternatives @@ big];
(Note: I have the definitions and the operations in difference cells, so I'm not constantly flipping the signs on all the y-values in big
)
This gets me close to what I'm looking for, but I don't think it's quite right. With a smaller list like my example it works, but when I'm using bigger lists (mine are 413 and 733 elements long), the resultant list is not the size of the smaller list: it comes out to 474 elements instead. Is there something wrong with what I'm doing, or do I probably just have a typo somewhere in my actual implementation?
small
guaranteed to exist as x-values inbig
? $\endgroup$