How can I add just the second elements in lists of pairs?
I like to do this kind of thing by writing a custom function to perform the the desired operation on two given pairs. I then map this function over the two lists with MapThread
. In most cases, by using Mathematica's argument-pattern destructuring, the custom function can be expressed extremely simply. This is one of those cases.
The custom function is
f[{x_, y_}, {_, z_}] := {x, y + z}
Note that the righthand side is a literal expression of the desired result.
Now let's contrive some test data. I write the generator so that two lists of pairs can easily be given any length.
With[{n = 3}, {a, b} = Partition[Partition[Range[4 n], 2], n]];
a
b
{{1, 2}, {3, 4}, {5, 6}} {{7, 8}, {9, 10}, {11, 12}}
Now we MapThread
the function f
over a
and b
.
MapThread[f, {a, b}]
{1, 10}, {3, 14}, {5, 18}}
lst1 = {{1, 2}, {2, 4}};
lst1a = lst1;
lst1a[[All, 2]] *= 2;
lst1a
{{1, 4}, {2, 8}}
lst2 = {{3, 5}, {10, 2}};
lst1b = lst1;
lst1b[[All, 2]] += lst2[[All, 2]];
lst1b
{{1, 7}, {2, 6}}
ClearAll[f1]
f1 = Module[{l = #}, l[[;; , 2]] += #2[[;; , 2]]; l] &;
f1[lst1, lst1]
{{1, 4}, {2, 8}}
f1[lst1, lst2]
{{1, 7}, {2, 6}}
f1[a, b] (* a and b from m_goldberg's answer *)
{{1, 10}, {3, 14}, {5, 18}}
Also
ClearAll[f2]
f2 = Module[{l2 = #2}, l2[[All, 1]] = 0; # + l2] &;
{f1[lst1, lst1] == f2[lst1, lst1], f1[lst1, lst2] == f2[lst1, lst2], f1[a, b] == f2[a, b]}
{True, True, True}
Try this:
{{1, 2}, {2, 4}} + ({{1, 2}, {2, 4}} /. {x_,y _} -> {0, y})
(* {{2, 4}, {4, 8}} *)
or this
(# + (# /. {x_, y_} -> {0, y})) &[{{1, 2}, {2, 4}}]
(* {{1, 2}, {4, 8}} *)
Have fun!