meshgrid

Function meshgrid 

Source
pub fn meshgrid<T: Meshgrid>(arrays: T, indexing: MeshIndex) -> T::Output
Expand description

Create coordinate matrices from coordinate vectors.

Given an N-tuple of 1D coordinate vectors, return an N-tuple of ND coordinate arrays. This is particularly useful for computing the outputs of functions with N arguments over regularly spaced grids.

The indexing argument can be controlled by MeshIndex to support both Cartesian and matrix indexing. In the two-dimensional case, inputs of length N and M will create output arrays of size (M, N) when using MeshIndex::XY and size (N, M) when using MeshIndex::IJ.

ยงExample

use ndarray::{array, meshgrid, MeshIndex};

let arr1 = array![1, 2];
let arr2 = array![3, 4];
let arr3 = array![5, 6];

// Cartesian indexing
let (res1, res2) = meshgrid((&arr1, &arr2), MeshIndex::XY);
assert_eq!(res1, array![
    [1, 2],
    [1, 2],
]);
assert_eq!(res2, array![
    [3, 3],
    [4, 4],
]);

// Matrix indexing
let (res1, res2) = meshgrid((&arr1, &arr2), MeshIndex::IJ);
assert_eq!(res1, array![
    [1, 1],
    [2, 2],
]);
assert_eq!(res2, array![
    [3, 4],
    [3, 4],
]);

let (_, _, res3) = meshgrid((&arr1, &arr2, &arr3), MeshIndex::XY);
assert_eq!(res3, array![
    [[5, 6],
     [5, 6]],
    [[5, 6],
     [5, 6]],
]);