mindspore.mint.meshgrid

View Source On Gitee
mindspore.mint.meshgrid(*tensors, indexing='ij')[source]

Generates coordinate matrices from given coordinate tensors.

Given N one-dimensional coordinate tensors, returns a tuple outputs of N N-D coordinate tensors for evaluating expressions on an N-D grid.

Warning

This is an experimental API that is subject to change or deletion.

Parameters

tensors (Union(tuple[Tensor], list[Tensor])) – In GRAPH_MODE, a tuple of N 1-D Tensor objects and the length of input should be greater than 1. In PYNATIVE_MODE, a tuple of N 0-D or 1-D Tensor objects and the length of input should be greater than 0. The data type is Number.

Keyword Arguments

indexing (str, optional) – Cartesian ('xy', default) or matrix ('ij') indexing of output. Valid options: xy' or 'ij'. In the 2-D case with inputs of length M and N, for 'xy' indexing, the shape of outputs is \((N, M)\) for 'ij' indexing, the shape of outputs is \((M, N)\). In the 3-D case with inputs of length M, N and P, for 'xy' indexing, the shape of outputs is \((N, M, P)\) and for 'ij' indexing, the shape of outputs is \((M, N, P)\). Default: 'ij' .

Returns

Tensors, a Tuple of N N-D Tensor objects. The data type is the same with the Inputs.

Raises
  • TypeError – If indexing is not a str or tensors is not a tuple.

  • ValueError – If indexing is neither 'xy' nor 'ij'.

Supported Platforms:

Ascend

Examples

>>> import numpy as np
>>> from mindspore import Tensor
>>> from mindspore import mint
>>> x = Tensor(np.array([1, 2, 3, 4]).astype(np.int32))
>>> y = Tensor(np.array([5, 6, 7]).astype(np.int32))
>>> z = Tensor(np.array([8, 9, 0, 1, 2]).astype(np.int32))
>>> output = mint.meshgrid(x, y, z, indexing='xy')
>>> print(output)
(Tensor(shape=[3, 4, 5], dtype=Int32, value=
 [[[1, 1, 1, 1, 1],
   [2, 2, 2, 2, 2],
   [3, 3, 3, 3, 3],
   [4, 4, 4, 4, 4]],
  [[1, 1, 1, 1, 1],
   [2, 2, 2, 2, 2],
   [3, 3, 3, 3, 3],
   [4, 4, 4, 4, 4]],
  [[1, 1, 1, 1, 1],
   [2, 2, 2, 2, 2],
   [3, 3, 3, 3, 3],
   [4, 4, 4, 4, 4]]]),
 Tensor(shape=[3, 4, 5], dtype=Int32, value=
 [[[5, 5, 5, 5, 5],
   [5, 5, 5, 5, 5],
   [5, 5, 5, 5, 5],
   [5, 5, 5, 5, 5]],
  [[6, 6, 6, 6, 6],
   [6, 6, 6, 6, 6],
   [6, 6, 6, 6, 6],
   [6, 6, 6, 6, 6]],
  [[7, 7, 7, 7, 7],
   [7, 7, 7, 7, 7],
   [7, 7, 7, 7, 7],
   [7, 7, 7, 7, 7]]]),
 Tensor(shape=[3, 4, 5], dtype=Int32, value=
 [[[8, 9, 0, 1, 2],
   [8, 9, 0, 1, 2],
   [8, 9, 0, 1, 2],
   [8, 9, 0, 1, 2]],
  [[8, 9, 0, 1, 2],
   [8, 9, 0, 1, 2],
   [8, 9, 0, 1, 2],
   [8, 9, 0, 1, 2]],
  [[8, 9, 0, 1, 2],
   [8, 9, 0, 1, 2],
   [8, 9, 0, 1, 2],
   [8, 9, 0, 1, 2]]]))