Mathematica: Iterating with 2 Lists

  • Context: Mathematica 
  • Thread starter Thread starter Sarah rob
  • Start date Start date
  • Tags Tags
    Mathematica
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 3K views
Sarah rob
Messages
16
Reaction score
0
If I have 2 list which are made up of pairs of numbers
e.g.
list1 = {{4, 5}, {2, 10}, {0, 15}}
list2 = {{5, 2}, {3, 7}, {1, 12}}
is there a way I can apply a function (that I have already created, f[x_]) on the lists which runs for e.g. 5 iterative steps with list1 then apply the same function to list2 for a further 5 iterations (iteration steps 6 - 10) , list1 for ieterations 11 - 15, list2 for steps 16 - 20 etc ...

I have been able to use one list using NestList but can't incorporate
alternating 2 list any suggestions ?
 
Physics news on Phys.org
It's not quite clear exactly what you want, but here's something to maybe get you started

Code:
In[1]:= repeat[list1_,list2_,n_,m_,f_]:=Module[{l1=list1,l2=list2},
                         Do[ Print[l1=Nest[f,l1,n]]; Print[l2=Nest[f,l2,n]],{m}];{l1,l2}]

It does m steps. Each time it nests f n times on list1 and list2.
I've added the Print[] statements so that you can see what's happening.
E.g.

Code:
In[2]:= repeat[{1,2},{3,4},5,2,f]
During evaluation of In[2]:= f[f[f[f[f[{1,2}]]]]]
During evaluation of In[2]:= f[f[f[f[f[{3,4}]]]]]
During evaluation of In[2]:= f[f[f[f[f[f[f[f[f[f[{1,2}]]]]]]]]]]
During evaluation of In[2]:= f[f[f[f[f[f[f[f[f[f[{3,4}]]]]]]]]]]
Out[2]= {f[f[f[f[f[f[f[f[f[f[{1,2}]]]]]]]]]],f[f[f[f[f[f[f[f[f[f[{3,4}]]]]]]]]]]}