Mathematica Simple problem in Mathematica involving lists

  • Thread starter Thread starter Shukie
  • Start date Start date
  • Tags Tags
    Mathematica
AI Thread Summary
The discussion revolves around efficiently creating a new list, L3, by pairing elements from two lists, L1 and L2, in Mathematica. The user initially attempts to use nested Table commands to generate L3, which results in high memory usage and performance issues. A more efficient approach is suggested using the concept of mapping functions, where a binary function can create ordered pairs from L1 and the elements of L2. The discussion highlights the use of the "Map" function to achieve this pairing without excessive memory consumption. Additionally, the "Thread" function is mentioned as a potentially useful alternative for this task. The user successfully implements a solution based on the suggestions provided.
Shukie
Messages
91
Reaction score
0
I have these two lists:

L1 = {a, b, c}
L2 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}

Now, my goal is to match up the first element of L1, which is a, to all the first elements of L2 and the second element of L1 (b) with all the second elements of L2 and the same with c. So I would get this:

L3 = {{a, 1}, {b, 2}, {c, 3}}, {{a, 4}, {b, 5}, {c, 6}}, {{a, 7}, {b, 8}, {c, 9}}

So L3 would consist of three separate lists. I can do this by using the command:

Table[Table[{L1[], L2[[k]][]}, {i, 1, 3}], {k, 1, 3}]

However, this takes up a lot of memory and almost freezes my computer. There must be a better way to do it?
 
Physics news on Phys.org
Shukie said:
I have these two lists:

L1 = {a, b, c}
L2 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}

Now, my goal is to match up the first element of L1, which is a, to all the first elements of L2 and the second element of L1 (b) with all the second elements of L2 and the same with c. So I would get this:

L3 = {{a, 1}, {b, 2}, {c, 3}}, {{a, 4}, {b, 5}, {c, 6}}, {{a, 7}, {b, 8}, {c, 9}}

So L3 would consist of three separate lists. I can do this by using the command:

Table[Table[{L1[], L2[[k]][]}, {i, 1, 3}], {k, 1, 3}]

However, this takes up a lot of memory and almost freezes my computer. There must be a better way to do it?


I don't know Mathematica, but mathematically, what you are doing is "mapping" an operation over a list.

That is, given a list {a, b, c} and an operation F you produce {F(a), F(b), F(c)}.
With a binary function, you can give two lists of arguments.
F mapped over {a, b, c} and {x, y, z} is {F(a,x), F(b,y), F(c,z)}

Given a function, and lists of values for each argument, the MAP operation should return a list of results. In your case, you want to make ordered pairs

Pair(x,y) just gives you the ordered pair (x,y)

Pair mapped to {a,b,c} and {1,2,3} thus gives {(a,1), (b,2), (c,3)}

Now define the function F(x) as Map(Pair, {a,b,c}, x) (I don't know the notation in Mathematica for this)

Map this F over the list of lists {{1,2,3},{4,5,6},{7,8,9}}

By the way, I am pretty sure you don't need to do this if you are trying to get arguments for Regress; you should be able to keep the x-values as {a,b,c} and the y-values as {1,2,3}, and {4,5,6} for the next month, and so on. But knowing how to map these lists together in Mathematica is still well worthwhile.

Cheers -- sylas
 
Here's something useful. Consider the "Thread" function.
 
Thanks Sylas, I got it done, as you noticed =) I still have a question for you in the other thread though!
 

Similar threads

Back
Top