4
$\begingroup$

Mathematica implementation of meshgrid in 2D is

meshgrid[x_List, y_List]:={ConstantArray[x,Length[x]],Transpose@ConstantArray[y,Length[y]]}.

Can any one help to have Mathematica meshgrid in 3D

$\endgroup$
0

1 Answer 1

3
$\begingroup$

You may use the following for any dimension:

meshgrid[x__?VectorQ] := Reverse[Transpose[Tuples[Reverse[{x}]]]]
$\endgroup$
2
  • $\begingroup$ Sorry, I don't have time to test it with Matlab at the moment. $\endgroup$ Nov 28, 2018 at 11:07
  • $\begingroup$ How can I fix this code using your comments, please help meshgrid[x_List, y_List, z_list] := {ConstantArray[x, Length[x]], Transpose@ConstantArray[y, Length[y]], Transpose@ConstantArray[z, Length[z]]} {xx, yy, zz} = meshgrid[Range[-2, 2, .1], Range[-4, 4, .2], Range[-6, 6, .2] ] c = xx*Exp[-xx^2 - yy^2] + 2 zz; pts = Flatten[{xx, yy, zz, c}]; ListPlot3D[pts, PlotRange -> All, AxesLabel -> Automatic, ImagePadding -> 20, Mesh -> 35, InterpolationOrder -> 2, ColorFunction -> "Rainbow", Boxed -> False] $\endgroup$
    – irondonio
    Aug 6, 2019 at 6:54

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.