0
$\begingroup$

This question relates to Add a vector to a list of vectors whereas here I want to add list of vectors to list of vectors recursively. Given:

m = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}
k = m

Adding each vector of m to each vector of k is done by

k = Transpose[# + Transpose[m]] & /@ k

resulting in

     {{{2, 0}, {1, 1}, {0, 0}, {1, -1}}, {{1, 1}, {0, 2}, {-1, 1}, {0,0}}, {{0, 0}, {-1, 1}, {-2, 0}, {-1, -1}}, {{1, -1}, {0,0}, {-1, -1}, {0, -2}}}

This is my new k and should go back into my code, so I can add the vectors m to each element of this new k. The new result starts with {{{{3,0},{2,1},{1,0),{2,-1}}... I tried

Table[k = (Transpose[# + Transpose[m]] & /@ k), {i, 2}]

But it does not work and then I get too confused. Any idea for this tree-like structure?

$\endgroup$

1 Answer 1

2
$\begingroup$

This can be done with Nest and Outer. If you need intermediate results, you would use NestList:

n = 1; (*number of recursions*)
m = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
Nest[Flatten[Outer[Plus, #, m, 1], 1] &, m, n]

enter image description here

An for n=2:

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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