Diagonal Arrays
The
diag(V, k=0)
function allows you to extract a diagonal from an array, as well as build diagonal arrays from one-dimensional arrays.
V
- An array-like object, two-dimensional or one-dimensional arrays, matrices, lists, or tuples, or any function or object with a method that returns a list or tuple.
k - index of the diagonal (optional).
The default is
k = 0
which corresponds to the main diagonal. A positive
k
value moves the diagonal up, a negative value moves it down.
The function returns array
NumPy (
ndarray
) - the specified array diagonal or a diagonal array from the specified one-dimensional array.