Movatterモバイル変換


[0]ホーム

URL:


SciPy

numpy.diag

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:
v:array_like

Ifv 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-thdiagonal.

k:int, optional

Diagonal in question. The default is 0. Usek>0 for diagonalsabove the main diagonal, andk<0 for diagonals below the maindiagonal.

Returns:
out:ndarray

The extracted diagonal or constructed diagonal array.

See also

diagonal
Return specified diagonals.
diagflat
Create a 2-D array with the flattened input as a diagonal.
trace
Sum along diagonals.
triu
Upper triangle of an array.
tril
Lower triangle of an array.

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]])

Previous topic

numpy.ogrid

Next topic

numpy.diagflat

Quick search

  • © Copyright 2008-2018, The SciPy community.
  • Last updated on Jul 24, 2018.
  • Created usingSphinx 1.6.6.

[8]ページ先頭

©2009-2025 Movatter.jp