3
$\begingroup$

I have two lists, for example (the real lists are much longer):

list1 = {1, 2, 3, 4, 5, 6, 7};
list2 = {9, 10, 11, 12, 13, 14, 15};

I want to create a single list by adding together different elements from different places in each list. For example,

list3 = (list1i+1 + list1i-1) * (list2i+1 + list2i-1)

where $i$ is the location of the number in the list, e.g. in list2, 12 would be $i=4$. I realize the first and last entries in list3 would either be wrong or non-existent. For example, the 2nd item in list3 would be $(3+1)(11+9)$ and the 3rd item would be $(4+2)(12+10)$.

I hope that makes sense and I appreciate any help.

$\endgroup$
  • $\begingroup$ What do you expect for the first element in list3? $\endgroup$ – DavidC Oct 30 '13 at 16:28
3
$\begingroup$

This will do it:

(list1[[3 ;;]] + list1[[;; -3]]) (list2[[3 ;;]] + list2[[;; -3]])

{80, 132, 192, 260, 336}

The smallest value of i has to be 2, and therefore list1[3;;]] represents all values the list of i+1 can assume. Similarly for i-1. The syntax is explained in the documentation for Part.

$\endgroup$
  • $\begingroup$ list1[[;; Length[list1] - 2]] can be written as list1[[;; -3]] $\endgroup$ – Simon Woods Oct 30 '13 at 16:20
  • $\begingroup$ @SimonWoods Ty. $\endgroup$ – C. E. Oct 30 '13 at 16:21
2
$\begingroup$

Or this:

Times @@ (ListCorrelate[{1, 0, 1}, #, {2, -2}, 0] & /@ {list1, list2})

(* {20, 80, 132, 192, 260, 336, 84} *)
$\endgroup$
1
$\begingroup$

Another way of doing it

list3 = (RotateLeft[list1]+RotateRight[list1])*(RotateLeft[list2]+RotateRight[list2])
$\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.