This is a pre-release version (latest). Go to latest (2.4.4)
{ } Raw JSON

bundles / numpy latest / numpy / triu_indices_from

_ArrayFunctionDispatcher

numpy:triu_indices_from

source: /dev/numpy/build-install/usr/lib/python3.14/site-packages/numpy/lib/_twodim_base_impl.py :1141

Signature

def   triu_indices_from ( arr k = 0 )

Summary

Return the indices for the upper-triangle of arr.

Extended Summary

See triu_indices for full details.

Parameters

arr : ndarray, shape(N, N)

The indices will be valid for square arrays.

k : int, optional

Diagonal offset (see triu for details).

Returns

triu_indices_from : tuple, shape(2) of ndarray, shape(N)

Indices for the upper-triangle of arr.

Examples

import numpy as np
Create a 4 by 4 array
a = np.arange(16).reshape(4, 4)
a
Pass the array to get the indices of the upper triangular elements.
triui = np.triu_indices_from(a)
triui
a[triui]
This is syntactic sugar for triu_indices().
np.triu_indices(a.shape[0])
Use the `k` parameter to return the indices for the upper triangular array from the k-th diagonal.
triuim1 = np.triu_indices_from(a, k=1)
a[triuim1]

See also

tril_indices_from
triu
triu_indices

Aliases

  • numpy.triu_indices_from