bundles / numpy 2.4.3 / numpy / mask_indices
function
numpy:mask_indices
Signature
def mask_indices ( n , mask_func , k = 0 ) Summary
Return the indices to access (n, n) arrays, given a masking function.
Extended Summary
Assume mask_func is a function that, for a square array a of size (n, n) with a possible offset argument k, when called as mask_func(a, k) returns a new array with zeros in certain locations (functions like triu or tril do precisely this). Then this function returns the indices where the non-zero values would be located.
Parameters
n: intThe returned indices will be valid to access arrays of shape (n, n).
mask_func: callableA function whose call signature is similar to that of
triu,tril. That is,mask_func(x, k)returns a boolean array, shaped likex.kis an optional argument to the function.k: scalarAn optional argument which is passed through to
mask_func. Functions liketriu,triltake a second argument that is interpreted as an offset.
Returns
indices: tuple of arrays.The
narrays of indices corresponding to the locations wheremask_func(np.ones((n, n)), k)is True.
Examples
import numpy as np
✓iu = np.mask_indices(3, np.triu)
✓a = np.arange(9).reshape(3, 3) a a[iu]✓
iu1 = np.mask_indices(3, np.triu, 1)
✓a[iu1]
✓See also
- tril
- tril_indices
- triu
- triu_indices
Aliases
-
numpy.mask_indices