3
$\begingroup$

I have two lists with same length:

list00={10, 6, 8, 6, 9, 7, 9, 5, 7, 10};
list0={0, 5, 2, 4, 3, 0, 3, 3, 0, 5};

and another list:

mainlist={1, 2, 4, 10};

I wish to sum multiplied corresponding elements of list00 and list0 according to mainlist:

sum=10*0+6*5+6*4+10*5;

my code is:

sum = 0; Do[  sum += list0[[i]]*list00[[i]]
, {i, mainlist}]

Question:

If I have two distinguishable and different mainlists for list00 and list0 seperately, how can we do this process (adding multiplied corresponded elements):

list00={10, 6, 8, 6, 9, 7, 9, 5, 7, 10};
list0={0, 5, 2, 4, 3, 0, 3, 3, 0, 5};

mainlist00={1, 2, 4, 10};
mainlist0={1, 3, 5, 8};

*desired result:* 10*0+6*2+6*3+10*3;
$\endgroup$
4
$\begingroup$
list00 = {10, 6, 8, 6, 9, 7, 9, 5, 7, 10};
list0 = {0, 5, 2, 4, 3, 0, 3, 3, 0, 5};

mainlist00 = {1, 2, 4, 10};
mainlist0 = {1, 3, 5, 8};

Times @@@ Transpose[{list00[[mainlist00]], list0[[mainlist0]]}] // Total

60

or using Dot

list00[[mainlist00]].list0[[mainlist0]]

60

With one "mainlist" it would be

mainlist = {1, 2, 4, 10};

Times @@@ Transpose[{list00, list0}][[mainlist]] // Total

104

Or

list00[[mainlist]].list0[[mainlist]]

104

$\endgroup$
  • $\begingroup$ +1 for the Dot method $\endgroup$ – Mr.Wizard Jul 15 '17 at 2:27
1
$\begingroup$
☺ = #[[#2]].#3[[#4]] & @@ # &;

☺ @ {list00, mainlist, list0, mainlist}

104

☺ @ {list00, mainlist00, list0, mainlist0}

60

$\endgroup$
  • $\begingroup$ ... this is the Dot product of the selected Parts of the of the two lists. $\endgroup$ – kglr Jul 14 '17 at 20:43

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.