Indexing Routines

cupy.take(a, indices, axis=None, out=None)[source]

Takes elements of an array at specified indices along an axis.

This is an implementation of “fancy indexing” at single axis.

This function does not support mode option.

  • a (cupy.ndarray) – Array to extract elements.
  • indices (int or array-like) – Indices of elements that this function takes.
  • axis (int) – The axis along which to select indices. The flattened input is used by default.
  • out (cupy.ndarray) – Output array. If provided, it should be of appropriate shape and dtype.

The result of fancy indexing.

Return type:


See also


cupy.diagonal(a, offset=0, axis1=0, axis2=1)[source]

Returns specified diagonals.

This function extracts the diagonals along two specified axes. The other axes are not changed. This function returns a writable view of this array as NumPy 1.10 will do.

  • a (cupy.ndarray) – Array from which the diagonals are taken.
  • offset (int) – Index of the diagonals. Zero indicates the main diagonals, a positive value upper diagonals, and a negative value lower diagonals.
  • axis1 (int) – The first axis to take diagonals from.
  • axis2 (int) – The second axis to take diagonals from.

A view of the diagonals of a.

Return type:


See also



Construct an open mesh from multiple sequences.

This function takes N 1-D sequences and returns N outputs with N dimensions each, such that the shape is 1 in all but one dimension and the dimension with the non-unit shape value cycles through all N dimensions.

Using ix_ one can quickly construct index arrays that will index the cross product. a[cupy.ix_([1,3],[2,5])] returns the array [[a[1,2] a[1,5]], [a[3,2] a[3,5]]].

Parameters:*args – 1-D sequences
Returns:N arrays with N dimensions each, with N the number of input sequences. Together these arrays form an open mesh.
Return type:tuple of ndarrays


>>> a = cupy.arange(10).reshape(2, 5)
>>> a
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])
>>> ixgrid = cupy.ix_([0,1], [2,4])
>>> ixgrid
       [1]]), array([[2, 4]]))

See also


cupy.fill_diagonal(a, val, wrap=False)[source]

Fill the main diagonal of the given array of any dimensionality.

For an array a with a.ndim > 2, the diagonal is the list of locations with indices a[i, i, ..., i] all identical. This function modifies the input array in-place, it does not return a value.

  • a (cupy.ndarray) – The array, at least 2-D.
  • val (scalar) – The value to be written on the diagonal. Its type must be compatible with that of the array a.
  • wrap (bool) – If specified, the diagonal is “wrapped” after N columns. This affects only tall matrices.


>>> a = cupy.zeros((3, 3), int)
>>> cupy.fill_diagonal(a, 5)
>>> a
array([[5, 0, 0],
       [0, 5, 0],
       [0, 0, 5]])