numpy.diag(v,k=0)[source]¶Extract a diagonal or construct a diagonal array.
See the more detailed documentation fornumpy.diagonal if you use thisfunction 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 youare using.
| Parameters: |
|
|---|---|
| Returns: |
|
See also
Examples
>>>x=np.arange(9).reshape((3,3))>>>xarray([[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]])