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

1. Mar 9, 2013

Sue Laplace

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

2. Mar 9, 2013

Hepth

Code (Text):
data = Flatten[Table[{x, t, F[x, t]}, {x, {1, 5}}, {t, {1, 3, 7}}],1]
Or for more general:
Code (Text):

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: Mar 9, 2013
3. Mar 9, 2013

Bill Simpson

4. Mar 23, 2013

Sue Laplace

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 (Text):
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 (Text):
{{{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 (Text):
MapThread[{xList[[#1]], tList[[#2]], F[[#2, #1]]} &, {{1, 2, 3}, {1, 2}}]
But I am not getting it done!

5. Mar 23, 2013

Staff: Mentor

Try:
Code (Text):
Flatten[Table[{xList[[i]],tList[[j]],F[[i,j]]},{i,3},{j,2}],1]