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 ..
(n, k=0, m=None)
| 1032 | |
| 1033 | @set_module('numpy') |
| 1034 | def triu_indices(n, k=0, m=None): |
| 1035 | """ |
| 1036 | Return the indices for the upper-triangle of an (n, m) array. |
| 1037 | |
| 1038 | Parameters |
| 1039 | ---------- |
| 1040 | n : int |
| 1041 | The size of the arrays for which the returned indices will |
| 1042 | be valid. |
| 1043 | k : int, optional |
| 1044 | Diagonal offset (see `triu` for details). |
| 1045 | m : int, optional |
| 1046 | .. versionadded:: 1.9.0 |
| 1047 | |
| 1048 | The column dimension of the arrays for which the returned |
| 1049 | arrays will be valid. |
| 1050 | By default `m` is taken equal to `n`. |
| 1051 | |
| 1052 | |
| 1053 | Returns |
| 1054 | ------- |
| 1055 | inds : tuple, shape(2) of ndarrays, shape(`n`) |
| 1056 | The indices for the triangle. The returned tuple contains two arrays, |
| 1057 | each with the indices along one dimension of the array. Can be used |
| 1058 | to slice a ndarray of shape(`n`, `n`). |
| 1059 | |
| 1060 | See also |
| 1061 | -------- |
| 1062 | tril_indices : similar function, for lower-triangular. |
| 1063 | mask_indices : generic function accepting an arbitrary mask function. |
| 1064 | triu, tril |
| 1065 | |
| 1066 | Notes |
| 1067 | ----- |
| 1068 | .. versionadded:: 1.4.0 |
| 1069 | |
| 1070 | Examples |
| 1071 | -------- |
| 1072 | Compute two different sets of indices to access 4x4 arrays, one for the |
| 1073 | upper triangular part starting at the main diagonal, and one starting two |
| 1074 | diagonals further right: |
| 1075 | |
| 1076 | >>> iu1 = np.triu_indices(4) |
| 1077 | >>> iu2 = np.triu_indices(4, 2) |
| 1078 | |
| 1079 | Here is how they can be used with a sample array: |
| 1080 | |
| 1081 | >>> a = np.arange(16).reshape(4, 4) |
| 1082 | >>> a |
| 1083 | array([[ 0, 1, 2, 3], |
| 1084 | [ 4, 5, 6, 7], |
| 1085 | [ 8, 9, 10, 11], |
| 1086 | [12, 13, 14, 15]]) |
| 1087 | |
| 1088 | Both for indexing: |
| 1089 | |
| 1090 | >>> a[iu1] |
| 1091 | array([ 0, 1, 2, ..., 10, 11, 15]) |