Simple problem in Mathematica involving lists

  • Mathematica
  • Thread starter Shukie
  • Start date
  • #1
96
0

Main Question or Discussion Point

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 seperate 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?
 

Answers and Replies

  • #2
sylas
Science Advisor
1,646
6
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 seperate 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
sylas
Science Advisor
1,646
6
Here's something useful. Consider the "Thread" function.
 
  • #4
96
0
Thanks Sylas, I got it done, as you noticed =) I still have a question for you in the other thread though!
 

Related Threads for: Simple problem in Mathematica involving lists

  • Last Post
Replies
4
Views
2K
Replies
22
Views
2K
Replies
1
Views
2K
Replies
2
Views
773
Replies
1
Views
2K
Replies
3
Views
15K
  • Last Post
Replies
3
Views
14K
Replies
2
Views
4K
Top