Skip to content

Instantly share code, notes, and snippets.

@singularitti
Created April 7, 2019 05:21
Show Gist options
  • Save singularitti/6908a9f7f9c4f917ae88dc69cbe2adf2 to your computer and use it in GitHub Desktop.
Save singularitti/6908a9f7f9c4f917ae88dc69cbe2adf2 to your computer and use it in GitHub Desktop.
Meshgrid on 3D #JavaScript #array
function meshgrid(xArray, yArray, zArray) {
/*
Here xArray, yArray, zArray should all be 1d arrays.
Then it returns 3 fortran-style 3D arrays, that is,
they are all column-major order:
http://www.wikiwand.com/en/Row-_and_column-major_order.
The returned xMesh, yMesh, and zMesh are all of shape
[zNum, xNum, yNum].
*/
let xNum = xArray.size;
let yNum = yArray.size;
let zNum = zArray.size;
let xMesh = reshape(tile(xArray, [zNum, yNum]), [zNum, xNum, yNum]);
let yMesh = reshape(tile(tile(yArray, [1, xNum]).transpose(1, 0), [zNum]), [zNum, xNum, yNum]);
let zMesh = tile(zArray, [1, xNum, yNum]);
return [xMesh, yMesh, zMesh];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment