Mathematica Mathematica: Iterating with 2 Lists

  • Thread starter Thread starter Sarah rob
  • Start date Start date
  • Tags Tags
    Mathematica
AI Thread Summary
The discussion centers on applying a function iteratively to two lists of number pairs in Mathematica. The user seeks a method to alternate iterations between the two lists, applying a function for a specified number of steps on each list. A suggested solution involves creating a custom function that utilizes Nest to perform the iterations on both lists in a loop. The provided code example demonstrates how to implement this alternating iteration effectively. The approach allows for clear tracking of the results after each iteration step.
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}]]]]]]]]]]}
 

Similar threads

Replies
1
Views
4K
Replies
2
Views
3K
Replies
6
Views
4K
Replies
5
Views
3K
Replies
2
Views
2K
Replies
4
Views
3K
Replies
22
Views
3K
Back
Top