bundles / numpy 2.4.3 / numpy / indices
function
numpy:indices
source: /numpy/_core/numeric.py :1790
Signature
def indices ( dimensions , dtype = <class 'int'> , sparse = False ) Summary
Return an array representing the indices of a grid.
Extended Summary
Compute an array where the subarrays contain index values 0, 1, ... varying only along the corresponding axis.
Parameters
dimensions: sequence of intsThe shape of the grid.
dtype: dtype, optionalData type of the result.
sparse: boolean, optionalReturn a sparse representation of the grid instead of a dense representation. Default is False.
Returns
grid: one ndarray or tuple of ndarraysIf sparse is False:
Returns one array of grid indices,
grid.shape = (len(dimensions),) + tuple(dimensions).If sparse is True:
Returns a tuple of arrays, with
grid[i].shape = (1, ..., 1, dimensions[i], 1, ..., 1)with dimensions[i] in the ith place
Notes
The output shape in the dense case is obtained by prepending the number of dimensions in front of the tuple of dimensions, i.e. if dimensions is a tuple (r0, ..., rN-1) of length N, the output shape is (N, r0, ..., rN-1).
The subarrays grid[k] contains the N-D array of indices along the k-th axis. Explicitly
grid[k, i0, i1, ..., iN-1] = ikExamples
import numpy as np grid = np.indices((2, 3)) grid.shape grid[0] # row indices grid[1] # column indices✓
x = np.arange(20).reshape(5, 4) row, col = np.indices((2, 3)) x[row, col]✓
i, j = np.indices((2, 3), sparse=True) i.shape j.shape i # row indices j # column indices✓
See also
- meshgrid
- mgrid
- ogrid
Aliases
-
numpy.indices