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

Click For Summary

Discussion Overview

The discussion revolves around how to create a 3D list from two 1D lists and a matrix of values in Mathematica. Participants explore methods for combining discrete data points into a format suitable for 3D plotting and curve fitting, focusing on the manipulation of lists and tables within the software.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Exploratory

Main Points Raised

  • One participant presents a specific data structure and requests assistance in transforming it into a 3D list format for plotting and fitting.
  • Another participant suggests using the Flatten and Table functions to create the desired structure, emphasizing the importance of the order of variables in the Table function.
  • A third participant proposes the use of the Outer function as a potential solution for constructing the required data format.
  • One participant clarifies that they do not have a formula for F(x,t) but rather a matrix of values, which complicates their ability to use certain functions effectively.
  • Another participant offers a specific code snippet using Flatten and Table to achieve the desired output format, addressing the challenge of combining the lists and matrix correctly.

Areas of Agreement / Disagreement

Participants present various methods and approaches, indicating that multiple competing views exist on how to achieve the desired data structure. There is no consensus on a single solution, as different participants propose different techniques and functions.

Contextual Notes

Some participants express limitations based on the absence of a functional form for F(x,t), which affects their ability to utilize certain Mathematica functions. Additionally, the discussion includes various assumptions about the structure and dimensions of the data being manipulated.

Who May Find This Useful

This discussion may be useful for Mathematica users dealing with data manipulation, particularly in the context of preparing data for 3D plotting and curve fitting in scientific or engineering applications.

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!
 
Try:
Code:
Flatten[Table[{xList[[i]],tList[[j]],F[[i,j]]},{i,3},{j,2}],1]
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 1 ·
Replies
1
Views
5K
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
10K
  • · Replies 3 ·
Replies
3
Views
17K
  • · Replies 2 ·
Replies
2
Views
2K