gather_nd#

ivy.gather_nd(params, indices, /, *, batch_dims=0, out=None)[source]#

Gather slices from params into a array with shape specified by indices.

Parameters:
  • params (Union[Array, NativeArray]) – The array from which to gather values.

  • indices (Union[Array, NativeArray]) – Index array.

  • batch_dims (int, default: 0) – optional int, lets you gather different items from each element of a batch.

  • out (Optional[Array], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

ret – New array of given shape, with the values gathered at the indices.

Examples

With ivy.Array input:

>>> x = ivy.array([0., 1., 2., 3., 4., 5., 6.])
>>> y = ivy.array([1])
>>> print(ivy.gather_nd(x, y))
ivy.array(1.)
>>> x = ivy.array([[0., 1.], [2., 3.], [4., 5.]])
>>> y = ivy.array([[0],[1],[1]], dtype='int32')
>>> z = ivy.gather_nd(x,y,batch_dims=1)
ivy.array([0., 3., 5.])

With a mix of ivy.Array and ivy.Container inputs:

>>> x = ivy.Container(a=ivy.array([0., 1., 2.]),b=ivy.array([4., 5., 6.]))
>>> y = ivy.array([1])
>>> print(ivy.gather_nd(x, y))
{
    a: ivy.array(1.),
    b: ivy.array(5.)
}

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([[0., 10., 20.],[30.,40.,50.]]),
...                   b=ivy.array([[0., 100., 200.],[300.,400.,500.]]))
>>> y = ivy.Container(a=ivy.array([1,0]),
...                   b=ivy.array([0]))
>>> print(ivy.gather_nd(x, y))
{
    a: ivy.array(30.),
    b: ivy.array([0., 100., 200.])
}
Array.gather_nd(self, indices, /, *, batch_dims=0, out=None)[source]#

ivy.Array instance method variant of ivy.gather_nd. This method simply wraps the function, and so the docstring for ivy.gather_nd also applies to this method with minimal changes.

Parameters:
  • self (Array) – The array from which to gather values.

  • indices (Union[Array, NativeArray]) – Index array.

  • batch_dims (int, default: 0) – optional int, lets you gather different items from each element of a batch.

  • out (Optional[Array], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

ret – New array of given shape, with the values gathered at the indices.

Examples

>>> x = ivy.array([1, 2, 3])
>>> y = ivy.array([1])
>>> z = x.gather_nd(y)
>>> print(z)
ivy.array(2)
Container.gather_nd(self, indices, /, *, batch_dims=0, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#

ivy.Container instance method variant of ivy.gather_nd. This method simply wraps the function, and so the docstring for ivy.gather_nd also applies to this method with minimal changes.

Parameters:
  • self (Container) – The container from which to gather values.

  • indices (Union[Container, Array, NativeArray]) – Index array or container.

  • batch_dims (Union[int, Container], default: 0) – optional int, lets you gather different items from each element of a batch.

  • key_chains (Optional[Union[List[str], Dict[str, str], Container]], default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (Union[bool, Container], default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (Union[bool, Container], default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (Union[bool, Container], default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

  • out (Optional[Container], default: None) – optional output container, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Container

Returns:

ret – New container of given shape, with the values gathered at the indices.

Examples

>>> x = ivy.Container(a=ivy.array([[[0., 10.], [20.,30.]],
...                                [[40.,50.], [60.,70.]]]),
...                   b=ivy.array([[[0., 100.], [200.,300.]],
...                                [[400.,500.],[600.,700.]]]))
>>> y = ivy.Container(a=ivy.array([1,0]),
...                   b=ivy.array([0]))
>>> z = x.gather_nd(y)
>>> print(z)
{
    a: ivy.array([40., 50.]),
    b: ivy.array([[0., 100.],
                [200., 300.]])
}