Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Simple problem in Mathematica involving lists

  1. May 2, 2009 #1
    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?
  2. jcsd
  3. May 2, 2009 #2


    User Avatar
    Science Advisor

    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
  4. May 2, 2009 #3


    User Avatar
    Science Advisor

    Here's something useful. Consider the "Thread" function.
  5. May 2, 2009 #4
    Thanks Sylas, I got it done, as you noticed =) I still have a question for you in the other thread though!
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Similar Threads for Simple problem Mathematica
Mathematica Storing Mathematica output
Mathematica Mathematica to MATLAB
LaTeX \vec command
MATLAB Does anyone had this plotting problem with MATLAB?
Mathematica Solve Perturbation problem with mathematica