I want to create a three dimensional grid (x,y,z) in mathematica and plot a function f(x,y,z) on it. I am not worried about the plotting part now; for that I will probably use ListContourPlot3D or something like that. But, I am having problem in creating the grid itself. In matlab, one can achieve this by Meshgrid command which takes either two or three variable. There are many nice ways to reproduce the two variable meshgrid equivalent in mathematica as discussed in this stackexchange question: Simulate MATLAB's meshgrid function

However, what should be the equivalent for 3D case? I am trying a combination of Riffle and Partition, but it's getting too long and confusing. Any help will be much appreciated. Thanks!!

  • $\begingroup$ So you just want an arbitrary data set of {x,y,z}? Like - data = RandomInteger[2, {5, 3}] ? Then in order to apply this arbitrary data to an arbitrary function use something like f @@@ data, where f is your function which takes on three parameters $\endgroup$ May 13, 2016 at 10:41
  • 1
    $\begingroup$ Table[]? Array[]? $\endgroup$ May 13, 2016 at 10:41

1 Answer 1


There are better ways to make this list that don't involve emulating MATLAB, but what you want is straightforward:

meshgrid3D[xxx_List, yyy_List, zzz_List] := 
 Table[#, {x, xxx}, {y, yyy}, {z, zzz}] & /@ {x, y, z}

{xxx, yyy, zzz} = 
  meshgrid3D[Range[-2, 2, .1], Range[-2, 2, .1], Range[-2, 2, .1]];

ListContourPlot3D[xxx^2 + yyy^2 - zzz^2, Contours -> {0}, 
 Mesh -> None]

Mathematica graphics

For example, it is better to use

grid3D = Table[{x, y, z}, {x, -2, 2, .1}, {y, -2, 2, .1}, {z, -2, 
    2, .1}];
func[x_, y_, z_] := x^2 + y^2 - z^2;
  grid3D, {3}], Contours -> {0}, Mesh -> None]
  • 2
    $\begingroup$ Consider Transpose[Outer[List, xxx, yyy, zzz], {2, 3, 4, 1}]. $\endgroup$ May 13, 2016 at 10:57
  • $\begingroup$ I wonder if the OP is aware that they can just write Table[func[x, y, z], {x, -2, 2, .1}, {y, -2, 2, .1}, {z, -2, 2, .1}]. $\endgroup$
    – user484
    May 13, 2016 at 13:16
  • 1
    $\begingroup$ It seemed the goal was to emulate the matlab way of making a 3d plot rather than making the plot the best way possible..... $\endgroup$
    – Jason B.
    May 13, 2016 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.