MCPcopy Create free account
hub / github.com/numpy/numpy / triu_indices_from

Function triu_indices_from

numpy/lib/twodim_base.py:1120–1183  ·  view source on GitHub ↗

Return the indices for the upper-triangle of arr. 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

(arr, k=0)

Source from the content-addressed store, hash-verified

1118
1119@array_function_dispatch(_trilu_indices_form_dispatcher)
1120def triu_indices_from(arr, k=0):
1121 """
1122 Return the indices for the upper-triangle of arr.
1123
1124 See `triu_indices` for full details.
1125
1126 Parameters
1127 ----------
1128 arr : ndarray, shape(N, N)
1129 The indices will be valid for square arrays.
1130 k : int, optional
1131 Diagonal offset (see `triu` for details).
1132
1133 Returns
1134 -------
1135 triu_indices_from : tuple, shape(2) of ndarray, shape(N)
1136 Indices for the upper-triangle of `arr`.
1137
1138 Examples
1139 --------
1140
1141 Create a 4 by 4 array.
1142
1143 >>> a = np.arange(16).reshape(4, 4)
1144 >>> a
1145 array([[ 0, 1, 2, 3],
1146 [ 4, 5, 6, 7],
1147 [ 8, 9, 10, 11],
1148 [12, 13, 14, 15]])
1149
1150 Pass the array to get the indices of the upper triangular elements.
1151
1152 >>> triui = np.triu_indices_from(a)
1153 >>> triui
1154 (array([0, 0, 0, 0, 1, 1, 1, 2, 2, 3]), array([0, 1, 2, 3, 1, 2, 3, 2, 3, 3]))
1155
1156 >>> a[triui]
1157 array([ 0, 1, 2, 3, 5, 6, 7, 10, 11, 15])
1158
1159 This is syntactic sugar for triu_indices().
1160
1161 >>> np.triu_indices(a.shape[0])
1162 (array([0, 0, 0, 0, 1, 1, 1, 2, 2, 3]), array([0, 1, 2, 3, 1, 2, 3, 2, 3, 3]))
1163
1164 Use the `k` parameter to return the indices for the upper triangular array
1165 from the k-th diagonal.
1166
1167 >>> triuim1 = np.triu_indices_from(a, k=1)
1168 >>> a[triuim1]
1169 array([ 1, 2, 3, 6, 7, 11])
1170
1171
1172 See Also
1173 --------
1174 triu_indices, triu, tril_indices_from
1175
1176 Notes
1177 -----

Callers

nothing calls this directly

Calls 1

triu_indicesFunction · 0.85

Tested by

no test coverage detected