Mathematica: Iterating with 2 Lists

  • Mathematica
  • Thread starter Sarah rob
  • Start date
  • Tags
    Mathematica
In summary, the repeat function takes two lists and applies a given function (f) for a specified number of iterations (n) on each list, alternating between the two lists. This can be done for a specified number of steps (m), with the function returning the final lists after all iterations are completed.
  • #1
Sarah rob
16
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
  • #2
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}]]]]]]]]]]}
 

1. What is Mathematica and why is it useful for iterating with 2 lists?

Mathematica is a computational software program used for mathematical and scientific calculations. It is useful for iterating with 2 lists because it has built-in functions and algorithms that make it easy to perform calculations and manipulate data in an efficient and accurate manner.

2. How do I iterate over 2 lists in Mathematica?

To iterate over 2 lists in Mathematica, you can use the built-in function MapThread. This function takes two lists as input and applies a given function to each corresponding element in the lists, creating a new list with the results. Alternatively, you can use a combination of Map and Apply functions to achieve the same result.

3. Can I iterate over more than 2 lists in Mathematica?

Yes, you can iterate over any number of lists in Mathematica using the MapThread function. You can also use the Map and Apply functions in combination with Nest or Fold functions to iterate over multiple lists and perform more complex operations.

4. How do I access specific elements in the 2 lists while iterating in Mathematica?

You can access specific elements in the 2 lists while iterating in Mathematica by using the Slot (#) notation. This allows you to refer to the current element in the list being iterated over. You can also use the Part ( [[ ]] ) notation to access specific elements in the lists by index.

5. Can I iterate over lists of different lengths in Mathematica?

Yes, you can iterate over lists of different lengths in Mathematica using the MapThread function. If the lists have different lengths, the function will only iterate over the elements that are present in all of the lists. You can also use the PadRight function to pad the shorter lists with a specific value before iterating.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
  • Set Theory, Logic, Probability, Statistics
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
261
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
277
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
  • Programming and Computer Science
Replies
29
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
Back
Top