Mathematica: Iterating with 2 Lists

  • Context: Mathematica 
  • Thread starter Thread starter Sarah rob
  • Start date Start date
  • Tags Tags
    Mathematica
Click For Summary
SUMMARY

The discussion focuses on iterating over two lists in Mathematica using a custom function, f[x_]. The user seeks to alternate iterations between two lists, list1 and list2, for a specified number of steps. A solution is provided using the repeat function, which employs Nest to apply f to each list for defined iterations. The example demonstrates the functionality of the repeat function, showing how it effectively alternates between the two lists over multiple iterations.

PREREQUISITES
  • Familiarity with Mathematica programming language
  • Understanding of the Nest and Do functions in Mathematica
  • Basic knowledge of functional programming concepts
  • Experience with list manipulation in Mathematica
NEXT STEPS
  • Explore advanced list manipulation techniques in Mathematica
  • Learn about the use of functional programming in Mathematica
  • Investigate the performance implications of using Nest versus other iteration methods
  • Study the creation of custom functions in Mathematica for complex data processing
USEFUL FOR

Mathematica users, data scientists, and programmers looking to enhance their skills in iterative list processing and functional programming within the Mathematica environment.

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 ·
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 22 ·
Replies
22
Views
4K