Skip to main content
replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/
Source Link

For the example you gave, with positive integer pairs as the first element and no duplication:

l1 = {{{1,2},1},{{3,4},2},{{1,3},3}};
l2 = {{{1,2},2},{{3,5},2},{{1,3},3}};

SetSystemOptions["SparseArrayOptions" -> {"TreatRepeatedEntries" -> Subtract}];

SparseArray[Rule @@@ Join[l1, l2]] // ArrayRules // Most

List @@@ %
{{1, 2} -> -1, {1, 3} -> 0, {3, 4} -> 2, {3, 5} -> 2}
{{{1, 2}, -1}, {{1, 3}, 0}, {{3, 4}, 2}, {{3, 5}, 2}}

Reference: Additive SparseArray AssemblyAdditive SparseArray Assembly

For the example you gave, with positive integer pairs as the first element and no duplication:

l1 = {{{1,2},1},{{3,4},2},{{1,3},3}};
l2 = {{{1,2},2},{{3,5},2},{{1,3},3}};

SetSystemOptions["SparseArrayOptions" -> {"TreatRepeatedEntries" -> Subtract}];

SparseArray[Rule @@@ Join[l1, l2]] // ArrayRules // Most

List @@@ %
{{1, 2} -> -1, {1, 3} -> 0, {3, 4} -> 2, {3, 5} -> 2}
{{{1, 2}, -1}, {{1, 3}, 0}, {{3, 4}, 2}, {{3, 5}, 2}}

Reference: Additive SparseArray Assembly

For the example you gave, with positive integer pairs as the first element and no duplication:

l1 = {{{1,2},1},{{3,4},2},{{1,3},3}};
l2 = {{{1,2},2},{{3,5},2},{{1,3},3}};

SetSystemOptions["SparseArrayOptions" -> {"TreatRepeatedEntries" -> Subtract}];

SparseArray[Rule @@@ Join[l1, l2]] // ArrayRules // Most

List @@@ %
{{1, 2} -> -1, {1, 3} -> 0, {3, 4} -> 2, {3, 5} -> 2}
{{{1, 2}, -1}, {{1, 3}, 0}, {{3, 4}, 2}, {{3, 5}, 2}}

Reference: Additive SparseArray Assembly

Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

For the example you gave, with positive integer pairs as the first element and no duplication:

l1 = {{{1,2},1},{{3,4},2},{{1,3},3}};
l2 = {{{1,2},2},{{3,5},2},{{1,3},3}};

SetSystemOptions["SparseArrayOptions" -> {"TreatRepeatedEntries" -> Subtract}];

SparseArray[Rule @@@ Join[l1, l2]] // ArrayRules // Most

List @@@ %
{{1, 2} -> -1, {1, 3} -> 0, {3, 4} -> 2, {3, 5} -> 2}
{{{1, 2}, -1}, {{1, 3}, 0}, {{3, 4}, 2}, {{3, 5}, 2}}

Reference: Additive SparseArray Assembly