numpy.diag
-
numpy.diag(v, k=0)
[source] -
Extract a diagonal or construct a diagonal array.
See the more detailed documentation for
numpy.diagonal
if you use this function to extract a diagonal and wish to write to the resulting array; whether it returns a copy or a view depends on what version of numpy you are using.- Parameters
-
-
varray_like
-
If
v
is a 2-D array, return a copy of itsk
-th diagonal. Ifv
is a 1-D array, return a 2-D array withv
on thek
-th diagonal. -
kint, optional
-
Diagonal in question. The default is 0. Use
k>0
for diagonals above the main diagonal, andk<0
for diagonals below the main diagonal.
-
- Returns
-
-
outndarray
-
The extracted diagonal or constructed diagonal array.
-
See also
Examples
>>> x = np.arange(9).reshape((3,3)) >>> x array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
>>> np.diag(x) array([0, 4, 8]) >>> np.diag(x, k=1) array([1, 5]) >>> np.diag(x, k=-1) array([3, 7])
>>> np.diag(np.diag(x)) array([[0, 0, 0], [0, 4, 0], [0, 0, 8]])
© 2005–2020 NumPy Developers
Licensed under the 3-clause BSD License.
https://numpy.org/doc/1.19/reference/generated/numpy.diag.html