meshgrid
Creates a pair of 2D coordinate matrices from two 1D coordinate vectors. Useful for evaluating functions on a grid or creating plotting meshes.
Signature
fun <T : Number> Multik.meshgrid(
x: MultiArray<T, D1>,
y: MultiArray<T, D1>
): Pair<D2Array<T>, D2Array<T>>
Parameters
Parameter | Type | Description |
|---|---|---|
|
| 1D array of x-coordinates. |
|
| 1D array of y-coordinates. |
Returns: Pair<D2Array<T>, D2Array<T>> — two matrices (X, Y) where:
Xhas shape(y.size, x.size)— each row is a copy ofx.Yhas shape(y.size, x.size)— each column is a copy ofy.
Example
val x = mk.ndarray(mk[1, 2, 3])
val y = mk.ndarray(mk[4, 5])
val (X, Y) = mk.meshgrid(x, y)
// X:
// [[1, 2, 3],
// [1, 2, 3]]
// Y:
// [[4, 4, 4],
// [5, 5, 5]]
This is useful for evaluating functions on a grid:
val x = mk.linspace<Double>(0.0, 1.0, 10)
val y = mk.linspace<Double>(0.0, 1.0, 10)
val (X, Y) = mk.meshgrid(x, y)
// Compute f(x, y) = x^2 + y^2 element-wise on the grid
28 February 2026