Mathematica:Iteration of pairs in a list

  • Mathematica
  • Thread starter Sarah rob
  • Start date
  • Tags
    List
In summary, the conversation discusses ways to combine two functions and create a list of pairs using Table and NestList. The output can also be generated using initial conditions and applying a function to the elements.
  • #1
Sarah rob
16
0
If I want to put these two functions
j[x_] := 2 x
k[x_] := 3 x
in pairs e.g. for the 1 st 4 terms of the functons i done
Table[{j[x], k[x]}, {x, 1, 4}]
to get
{{2, 3}, {4, 6}, {6, 9}, {8, 12}}
However i want to run this for several interations so that I get
{{2, 3}, {4, 6}, {6, 9}, {8, 12}}
{{4, 9}, {8, 18}, {12, 27}, {16, 32}}
{{8, 27}, {16, 54}, {24, 81}, {32, 96}}
etc ...
I am trying to use NestList but it doesn' t work with Table, is there
another way other than Table I can combine the functions
 
Physics news on Phys.org
  • #2
The obstacle is that the first time you go through the iteration, you feed j[x] and k[x] the same number. The subsequent times you must feed them the different elements in a list of pairs.

An ugly way is something like
Code:
In[1]:= j[x_]:=2 x
        k[x_]:=3 x
In[3]:= iter1=Table[{j[x],k[x]},{x,1,4}]
Out[3]= {{2,3},{4,6},{6,9},{8,12}}
In[4]:= iter2=Table[{j[#1],k[#2]}&@@x, {x,iter1}]
Out[4]= {{4,9},{8,18},{12,27},{16,36}}
In[5]:= iter3=Table[{j[#1],k[#2]}&@@x, {x,iter2}]
Out[5]= {{8,27},{16,54},{24,81},{32,108}}

If you just want to create the lists that you want, then maybe something like
Code:
In[6]:= Table[{2^n i,3^n i},{n,0,3},{i,1,4}]//Grid
Out[6]= {1,1}	{2,2}	{3,3}	{4,4}
        {2,3}	{4,6}	{6,9}	{8,12}
        {4,9}	{8,18}	{12,27}	{16,36}
        {8,27}	{16,54}	{24,81}	{32,108}

The same output can be created using NestList, e.g.
Code:
NestList[{j@#1,k@#2}&@@@#&,{Range[1,4],Range[1,4]}\[Transpose],3]//Grid
or
Code:
NestList[{2,3}#&/@#&,{Range[1,4],Range[1,4]}\[Transpose],3]//Grid
etc... none of the NestList solutions I came up with were particularly satisfactory.
 
  • #3
Thanks for that.
Using one of the codes you suggested, is there a way to implement it with initial conditions, so starting with pairs of nuumbers and applying a function to all the 1st elements and another function to and the 2nd elements in the list.
 
  • #4
Let's say I want twice the first element and half the second.

t = Table[{2^n i, 3^n i}, {n, 0, 3}, {i, 1, 4}];
funpair[list_]:={2list[[1]], list[[2]]/2};
Table[t[[j, i]]//funpair, {j, 1, 4}, {i, 1, 4}]

generates:

{
{{2, 1/2}, {4, 1}, {6, 3/2}, {8, 2}},
{{4, 3/2}, {8, 3}, {12, 9/2}, {16, 6}},
{{8, 9/2}, {16, 9}, {24, 27/2}, {32, 18}},
{{16, 27/2}, {32, 27}, {48, 81/2}, {64, 54}}
}
 
  • #5


One possible way to achieve this is to use a combination of NestList and Map. First, define a function that takes in a list of pairs and applies the functions j and k to each pair:

applyFunctions[list_] := Map[{j[#[[1]]], k[#[[2]]]} &, list]

Then, use NestList to repeatedly apply this function to the initial list of pairs:

NestList[applyFunctions, {{2,3}, {4,6}, {6,9}, {8,12}}, 3]

This will give you the desired result of {{2, 3}, {4, 6}, {6, 9}, {8, 12}} {{4, 9}, {8, 18}, {12, 27}, {16, 32}} {{8, 27}, {16, 54}, {24, 81}, {32, 96}}.
Alternatively, you can use the built-in function NestList to directly iterate over a function, without the need for defining a separate function. For example:

NestList[{j[#[[1]]], k[#[[2]]]} &, {{2,3}, {4,6}, {6,9}, {8,12}}, 3]

Both of these methods will give you the same result, and you can adjust the number of iterations as needed.
 

1. How do I iterate through pairs in a list using Mathematica?

In Mathematica, you can use the MapIndexed function to iterate through pairs in a list. This function takes in a function and a list as arguments, and applies the function to each element in the list along with its index.

2. Can I iterate through pairs in a list using a specific pattern?

Yes, you can use the Cases function in Mathematica to iterate through pairs in a list that match a specific pattern. This function takes in a pattern and a list as arguments, and returns the elements in the list that match the given pattern.

3. How can I access the elements in a pair while iterating through a list?

You can use the Part function in Mathematica to access the elements in a pair while iterating through a list. This function takes in a list and an index as arguments, and returns the element at the given index in the list.

4. Is it possible to iterate through only a certain number of pairs in a list?

Yes, you can use the Take function in Mathematica to iterate through only a certain number of pairs in a list. This function takes in a list and a number as arguments, and returns a new list containing the first n elements from the original list.

5. Can I iterate through pairs in a list in reverse order?

Yes, you can use the Reverse function in Mathematica to iterate through pairs in a list in reverse order. This function takes in a list as an argument, and returns a new list with the elements in reverse order.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
340
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • Precalculus Mathematics Homework Help
Replies
11
Views
722
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
815
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
547
Back
Top