Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
    
What do you expect for the first element in list3? –  David Carraher Oct 30 '13 at 16:28

3 Answers 3

up vote 3 down vote accepted

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.

share|improve this answer
    
list1[[;; Length[list1] - 2]] can be written as list1[[;; -3]] –  Simon Woods Oct 30 '13 at 16:20
    
@SimonWoods Ty. –  Pickett Oct 30 '13 at 16:21

Or this:

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

(* {20, 80, 132, 192, 260, 336, 84} *)
share|improve this answer

Another way of doing it

list3 = (RotateLeft[list1]+RotateRight[list1])*(RotateLeft[list2]+RotateRight[list2])
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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