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

In summary, the user is trying to combine a table of data with a function that will produce a three dimensional plot.
  • #1
Sue Laplace
11
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
  • #2
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:
  • #3
  • #4
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!
 
  • #5
Try:
Code:
Flatten[Table[{xList[[i]],tList[[j]],F[[i,j]]},{i,3},{j,2}],1]
 

1. How can I create a 3D list in Mathematica using two 1D lists and a table?

To create a 3D list in Mathematica, you can use the "Table" function. Within the "Table" function, you can specify the dimensions of your list and use the "Join" function to combine two 1D lists.

2. Can I use different types of data in my 3D list?

Yes, you can use any type of data in your 3D list in Mathematica. This includes numbers, strings, and even other lists.

3. How do I access specific elements in a 3D list?

To access specific elements in a 3D list, you can use double brackets after the list name. For example, if your list is named "myList", you can access the element in the first row, second column, and third layer by using myList[[1,2,3]].

4. Can I apply functions to a 3D list in Mathematica?

Yes, you can apply functions to a 3D list in Mathematica. You can use the "Map" or "Apply" functions to apply a function to each element in the list, or you can use the "Table" function to apply a function to each element in a specific dimension of the list.

5. How can I visualize my 3D list in Mathematica?

You can use the "ListPlot3D" function in Mathematica to create a 3D plot of your list. This will allow you to see the data in a visual format and potentially identify any patterns or trends.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
217
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
15
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
943
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
Replies
0
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
9K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
Back
Top