# Create 3D Graph from 3 Arrays

Posted 9 years ago
2084 Views
|
|
0 Total Likes
|
 Hello - I am trying to create a 3D graphic representation of some data. Right now I have a 12x50 matrix with values I wish to represent on the z-axis. I can easily use ListPlot3D to create a surface from these points using the array indices as x and y values. However, I wish to supply two additional lists to be the x and y values. Essentially, I want to have irregular spacing. What is the easiest way to do this?Thanks!
 Hello, The function MapIndexed can map a function onto a list or array, while passing to the function the part specification of each element it receives as an argument. The code below generates a sample 2D array from previously defined x and y value lists. it then uses MapIndexed to construct a list of {x,y,z} values, where the z values are obtained from the 2D array and the x and y values are obtained from the corresponding values in the x and y value lists. (* some x and y coordinates *) xList = {0, 1, 3, 4, 5, 7, 8}/4.; yList = {1, 3, 4, 7, 9}/4.; (* make pairs so we can make some data*) pairs = Tuples[{xList, yList}]; (* make sample 2D data related to the pairs as an example case *) data = Table[Sin[x y], {x, xList}, {y, yList}]; (* Look up the documentaton on MapIndexed -- this is a function which \ accepts a value in an array and also makes use of the part \ specifications as provided by MapIndexed *) makeTriplet[z_, {nx_, ny_}] := {xList[[nx]], yList[[ny]], z} (* Use map indexed to create a list of {x,y,z} triplets using the z \ values in the 2D array, but the x and y values from separate lists *) indexedData = Flatten[MapIndexed[makeTriplet, data, {2}], 1]; (* Plot the result and see that it makes sense *) ListPlot3D[indexedData] 