numpy.diag

numpy.diag(v, k=0)

Extract a diagonal or construct a diagonal array.

Parameters:

v : array_like

If v is a 2-dimensional array, return a copy of its k-th diagonal. If v is a 1-dimensional array, return a 2-dimensional array with v on the k-th diagonal.

k : int, optional

Diagonal in question. The defaults is 0.

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(np.diag(x))
array([[0, 0, 0],
       [0, 4, 0],
       [0, 0, 8]])

Previous topic

numpy.compress

Next topic

numpy.diagonal

This Page

Quick search