Python’s diagflat()
function creates a 2D
array with the flattened input as a diagonal.
A flattened array is simply a 1D
array. Therefore in this case of the diagflat()
function, the input data is flattened (converted to a 1D
array) and then used as the diagonal of the output 2D
array.
numpy.diagflat(v, k=0)
The diagflat()
function takes the following parameter values:
v
: This represents the array_like
object which is flattened and set as the k-th diagonal of the output.k
: This represents the diagonal to set, 0
is the main diagonal, or the default diagonal, a positive or negative k
is the number of diagonals above or below the main diagonal, respectively. This is optional.The diagflat()
function returns the 2D
output array.
import numpy as np# creating an arraymyarray = np.array([[1,2], [3,4]])# implementing the diagflat() functiondiagarray = np.diagflat(myarray, 1)print(myarray)print(diagarray)
numpy
library.myarray
, using the array()
function. diagflat()
function on the given array, myarray
, with the diagonal to start above the main diagonal at 1
. The result is assigned to a new variable daigarray
.myarray
.2D
diagonal array, diagarray
.