diff options
| author | Matti Picus <matti.picus@gmail.com> | 2023-01-26 12:35:07 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-26 12:35:07 +0200 |
| commit | 2289292db6a19f2bbfddd3dea3790ffa19955333 (patch) | |
| tree | debaa703240e46a10dd3a0bd436d5ead056ffd95 /numpy/lib | |
| parent | 946750554720b4fad163eb9d8f44833b0857d64f (diff) | |
| parent | 1bff4d246ed79d01522085136a7b6b46145b8f0e (diff) | |
| download | numpy-2289292db6a19f2bbfddd3dea3790ffa19955333.tar.gz | |
Merge pull request #22559 from richierocks/document_diag_indices_from
DOC #22266 Add examples for diag_indices_from()
Diffstat (limited to 'numpy/lib')
| -rw-r--r-- | numpy/lib/index_tricks.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/numpy/lib/index_tricks.py b/numpy/lib/index_tricks.py index 58dd394e1..fa3a90e4f 100644 --- a/numpy/lib/index_tricks.py +++ b/numpy/lib/index_tricks.py @@ -1009,6 +1009,32 @@ def diag_indices_from(arr): ----- .. versionadded:: 1.4.0 + Examples + -------- + + Create a 4 by 4 array. + + >>> a = np.arange(16).reshape(4, 4) + >>> a + array([[ 0, 1, 2, 3], + [ 4, 5, 6, 7], + [ 8, 9, 10, 11], + [12, 13, 14, 15]]) + + Get the indices of the diagonal elements. + + >>> di = np.diag_indices_from(a) + >>> di + (array([0, 1, 2, 3]), array([0, 1, 2, 3])) + + >>> a[di] + array([ 0, 5, 10, 15]) + + This is simply syntactic sugar for diag_indices. + + >>> np.diag_indices(a.shape[0]) + (array([0, 1, 2, 3]), array([0, 1, 2, 3])) + """ if not arr.ndim >= 2: |
