numpy.diagflat — NumPy v2.2 Manual (original) (raw)
numpy.diagflat(v, k=0)[source]#
Create a two-dimensional array with the flattened input as a diagonal.
Parameters:
varray_like
Input data, which is flattened and set as the _k_-th diagonal of the output.
kint, optional
Diagonal to set; 0, the default, corresponds to the “main” diagonal, a positive (negative) k giving the number of the diagonal above (below) the main.
Returns:
outndarray
The 2-D output array.
See also
MATLAB work-alike for 1-D and 2-D arrays.
Return specified diagonals.
Sum along diagonals.
Examples
import numpy as np np.diagflat([[1,2], [3,4]]) array([[1, 0, 0, 0], [0, 2, 0, 0], [0, 0, 3, 0], [0, 0, 0, 4]])
np.diagflat([1,2], 1) array([[0, 1, 0], [0, 0, 2], [0, 0, 0]])