3
$\begingroup$

I imported three lists

list1={{0.01, 0., 0.00242439}, {0.06, 0., 0.00228538}, 
       {0.11, 0.,  0.0018987}, {0.16, 0., 0.00148493}, 
       {0.21, 0., 0.00104764}, {0.26, 0., 0.000575535}, 
       {0.31, 0., 0.000068559}};

list2={{0.01, 0., 0.00242439}, {0.06, 0., 0.00237076}, 
       {0.11, 0., 0.00211971}, {0.16, 0., 0.001817}, 
       {0.21, 0., 0.00150603}, {0.26, 0., 0.00118223}, 
       {0.31, 0., 0.000839701}};

list3={{0.01, 0., 0.00242439}, {0.06, 0., 0.00240639},
      {0.11, 0., 0.00225701}, {0.16, 0., 0.00203365},
      {0.21, 0., 0.00179651}, {0.26, 0., 0.00155504},
      {0.31, 0., 0.00130653}};

I wish to add the third column of all lists with each other and keep unchanged two same columns. For example:

    result= {{0.01, 0., 0.00727317},{0.06, 0., 0.00706253},...}

How do I reach this goal?

$\endgroup$
8
$\begingroup$
list4 = list1;
list4[[All, 3]] = Total[{list1, list2, list3}[[All, All, 3]]];
list4

{{0.01,0.,0.00727317},{0.06,0.,0.00706253},{0.11,0.,0.00627542},{0.16,0.,0.00533558},{0.21,0.,0.00435018},{0.26,0.,0.00331281},{0.31,0.,0.00221479}}

Also

KeyValueMap[Append]@Merge[Total][#[[;;2]]->#[[3]]&/@Join[list1,list2,list3]]

same result

$\endgroup$
6
$\begingroup$
{#1/3, #2/3, #3} & @@@ (list1 + list2 + list3)

{{0.01, 0., 0.00727317}, {0.06, 0., 0.00706253}, {0.11, 0., 0.00627542}, {0.16, 0., 0.00533558}, {0.21, 0., 0.00435018}, {0.26, 0., 0.00331281}, {0.31, 0., 0.00221479}}

$\endgroup$
3
$\begingroup$

This is a great place to use GroupBy: Join everything together, then group by Most and make lists of all Last elements in a group, then reduce these lists with Total:

GroupBy[Join @@ {list1, list2, list3}, Most -> Last, Total]

<|{0.01, 0.} -> 0.00727317, {0.06, 0.} -> 0.00706253, {0.11, 0.} -> 0.00627542, {0.16, 0.} -> 0.00533558, {0.21, 0.} -> 0.00435018, {0.26, 0.} -> 0.00331281, {0.31, 0.} -> 0.00221479|>

To get to the OP's shape requirements, use @kglr's trick:

KeyValueMap[Append] @ GroupBy[Join @@ {list1, list2, list3}, Most -> Last, Total]

{{0.01, 0., 0.00727317}, {0.06, 0., 0.00706253}, {0.11, 0., 0.00627542}, {0.16, 0., 0.00533558}, {0.21, 0., 0.00435018}, {0.26, 0., 0.00331281}, {0.31, 0., 0.00221479}}

$\endgroup$
1
$\begingroup$
list1 + (list2+list3).DiagonalMatrix[{0,0,1}] 

{

{0.01, 0., 0.00727317}, {0.06, 0., 0.00706253},

{0.11, 0., 0.00627542}, {0.16, 0., 0.00533558},

{0.21, 0., 0.00435018}, {0.26, 0., 0.00331281},

{0.31, 0., 0.00221479}

}

In addition:

list1+Inner[Times, (list2+list3), {0,0,1}, List]

Original Answer

list1 + list2.DiagonalMatrix[{0, 0, 1}]+ list3.DiagonalMatrix[{0, 0, 1}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.