Mathematica: Make a 3D List from two 1D lists and a tables

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
4 replies · 5K views
Sue Laplace
Messages
10
Reaction score
0
I am dealing with a set of data where I have values C(x,t) for a discrete set of x and t values.
Say:
x={1,5}
t={1,3,7}
C={{C(1,1),C(1,3),C(1,7)},{C(5,1),C(5,3),C(5,7)}}.

How do I combine this to a list that looks like:
{{{1,1,C(1,1)},{1,3,C(1,3)},{1,7,C(1,7)},{5,1,C(5,1)},{5,3,C(5,3)},{5,7,C(5,7)}} so that I can make a 3D plot and do a 3D curve fitting?

Sue
 
Physics news on Phys.org
Code:
data = Flatten[Table[{x, t, F[x, t]}, {x, {1, 5}}, {t, {1, 3, 7}}],1]

Or for more general:
Code:
xList = {1, 5};
tList = {1, 3, 7};
data = Flatten[Table[{x, t, F[x, t]}, {x, xList}, {t, tList}],1]


Why:

Table creates the matrix/table structure. You can do multiple variables at once, and their order matters.
When doing any look function you can tell it how you want to iterate. It can be integral:

{i,1,10} ( i goes from 1 to 10 )

or multiple

{i,1,10,2} ( i goes from 1 to 10 by two )

and in this case exhaustive list

{i , {1,3,5,7,8}} (i takes each of the listed values )

There's more too, but just thought I'd share.

Also, Flatten is used as when you cycle over two variables, its going to make a n*m deep array, when we really want an n+m 1D array, so in this case your "x" values are separated into two tables. This creates a problem when trying to fit or plot as you want them to all be in one table usually. So we "Flatten" it to dimension 1. Just like joining two sets.

{{a,b},{a2,b2}},{{c,d}} -> {{a,b},{a2,b2},{c,d}}
 
Last edited:
Hi, Thank you for the reply.

The thing is, this seems only to work when you have actually got the formula F(x,t) which I don't, I only have a set of data telling me what F is for a certain number of x and t, hence I want to do the curve fitting. So, F is a two dimensional matrix. I am able to add either the t or the x values in a list together with F as follows:

Code:
F = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

xList = {x1, x2, x3};

tList = {t1, t2};

In[4]:= Map[Transpose[{xList, F[[#, All]]}] &, {1, 2}]

Out[4]= {{{x1, 1}, {x2, 2}, {x3, 3}}, {{x1, 4}, {x2, 5}, {x3, 6}}}

Now then I want to do something that will give me:

Code:
{{{t1,x1, 1}, {t1,x2, 2}, {t1,x3, 3}}, {{t2,x1, 4}, {t2,x2, 5}, {t2,x3, 6}}}

I have also been trying something like:

Code:
MapThread[{xList[[#1]], tList[[#2]], F[[#2, #1]]} &, {{1, 2, 3}, {1, 2}}]

But I am not getting it done!