Skip to content

Instantly share code, notes, and snippets.

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