Simple problem in Mathematica involving lists

  • Mathematica
  • Thread starter Shukie
  • Start date
  • Tags
    Mathematica
In summary, the conversation discusses a method for matching elements from two lists and creating a new list with ordered pairs. The method involves using a mapping function and the "Thread" function in Mathematica.
  • #1
Shukie
95
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
  • #2
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
 
  • #3
Here's something useful. Consider the "Thread" function.
 
  • #4
Thanks Sylas, I got it done, as you noticed =) I still have a question for you in the other thread though!
 

1. How do I create a list in Mathematica?

To create a list in Mathematica, use the curly brackets { } and separate each element with a comma. For example, {1, 2, 3} creates a list with the numbers 1, 2, and 3 in it.

2. How can I access specific elements in a list?

To access a specific element in a list, use double brackets [[ ]] and specify the position of the element you want to access. For example, myList[[2]] would access the second element in the list named "myList".

3. How do I add elements to an existing list?

To add elements to an existing list, use the Append function and specify the list and the element you want to add. For example, Append[myList, 4] would add the number 4 to the end of the list named "myList".

4. How can I perform operations on all elements in a list?

To perform operations on all elements in a list, use the Map function and specify the operation you want to perform and the list. For example, Map[Sqrt, myList] would take the square root of each element in the list named "myList".

5. How do I find the length of a list?

To find the length of a list, use the Length function and specify the list. For example, Length[myList] would return the number of elements in the list named "myList".

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Replies
1
Views
705
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
352
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
259
  • Programming and Computer Science
Replies
1
Views
768
  • Quantum Physics
Replies
2
Views
770
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Back
Top