Simple problem in Mathematica involving lists

  • Context: Mathematica 
  • Thread starter Thread starter Shukie
  • Start date Start date
  • Tags Tags
    Mathematica
Click For Summary

Discussion Overview

The discussion revolves around a problem in Mathematica involving the manipulation of two lists, L1 and L2, to create a new list L3 that matches elements from L1 with corresponding elements from L2. Participants explore methods to achieve this efficiently without consuming excessive memory.

Discussion Character

  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant describes their goal of matching elements from L1 = {a, b, c} with elements from L2 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} to create L3, which consists of ordered pairs.
  • The same participant shares their initial approach using nested Table commands in Mathematica, noting that it consumes a lot of memory and slows down their computer.
  • Another participant, who does not know Mathematica, discusses the mathematical concept of "mapping" operations over lists and suggests a method to create ordered pairs using a binary function.
  • This second participant proposes defining a function F that maps the Pair operation over the elements of L1 and L2, although they are uncertain about the specific Mathematica syntax.
  • A later reply introduces the "Thread" function as a potentially useful tool for the task at hand.
  • The original poster acknowledges the help received and mentions successfully completing the task, while also indicating they have further questions in another thread.

Areas of Agreement / Disagreement

Participants generally agree on the goal of matching elements from the two lists and explore different methods to achieve this. However, there is no consensus on the most efficient approach, as different strategies are proposed without resolving which is superior.

Contextual Notes

The discussion includes various assumptions about the capabilities of Mathematica and the efficiency of different functions, which are not fully explored or resolved.

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

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
10
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
1K