MCPcopy Index your code
hub / github.com/numpy/numpy / triu_indices

Function triu_indices

numpy/lib/_twodim_base_impl.py:1082–1179  ·  view source on GitHub ↗

Return the indices for the upper-triangle of an (n, m) array. Parameters ---------- n : int The size of the arrays for which the returned indices will be valid. k : int, optional Diagonal offset (see `triu` for details). m : int, optional The

(n, k=0, m=None)

Source from the content-addressed store, hash-verified

1080
1081@set_module('numpy')
1082def triu_indices(n, k=0, m=None):
1083 """
1084 Return the indices for the upper-triangle of an (n, m) array.
1085
1086 Parameters
1087 ----------
1088 n : int
1089 The size of the arrays for which the returned indices will
1090 be valid.
1091 k : int, optional
1092 Diagonal offset (see `triu` for details).
1093 m : int, optional
1094 The column dimension of the arrays for which the returned
1095 arrays will be valid.
1096 By default `m` is taken equal to `n`.
1097
1098
1099 Returns
1100 -------
1101 inds : tuple, shape(2) of ndarrays, shape(`n`)
1102 The row and column indices, respectively. The row indices are sorted
1103 in non-decreasing order, and the corresponding column indices are
1104 strictly increasing for each row.
1105
1106 See also
1107 --------
1108 tril_indices : similar function, for lower-triangular.
1109 mask_indices : generic function accepting an arbitrary mask function.
1110 triu, tril
1111
1112 Examples
1113 --------
1114 >>> import numpy as np
1115
1116 Compute two different sets of indices to access 4x4 arrays, one for the
1117 upper triangular part starting at the main diagonal, and one starting two
1118 diagonals further right:
1119
1120 >>> iu1 = np.triu_indices(4)
1121 >>> iu1
1122 (array([0, 0, 0, 0, 1, 1, 1, 2, 2, 3]), array([0, 1, 2, 3, 1, 2, 3, 2, 3, 3]))
1123
1124 Note that row indices (first array) are non-decreasing, and the corresponding
1125 column indices (second array) are strictly increasing for each row.
1126
1127 Here is how they can be used with a sample array:
1128
1129 >>> a = np.arange(16).reshape(4, 4)
1130 >>> a
1131 array([[ 0, 1, 2, 3],
1132 [ 4, 5, 6, 7],
1133 [ 8, 9, 10, 11],
1134 [12, 13, 14, 15]])
1135
1136 Both for indexing:
1137
1138 >>> a[iu1]
1139 array([ 0, 1, 2, ..., 10, 11, 15])

Callers 2

test_triu_indicesMethod · 0.90
triu_indices_fromFunction · 0.85

Calls 4

broadcast_toFunction · 0.90
indicesFunction · 0.90
triFunction · 0.85
indexMethod · 0.45

Tested by 1

test_triu_indicesMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…