Python | numpy.fill_diagonal() method (original) (raw)
Last Updated : 27 Sep, 2019
With the help of **numpy.fill_diagonal()**
method, we can get filled the diagonals of numpy array with the value passed as the parameter in numpy.fill_diagonal()
method.
Syntax :
numpy.fill_diagonal(array, value)
Return : Return the filled value in the diagonal of an array.
Example #1 :
In this example we can see that by using numpy.fill_diagonal()
method, we are able to get the diagonals filled with the values passed as parameter.
import
numpy as np
array
=
np.array([[
1
,
2
], [
2
,
1
]])
np.fill_diagonal(array,
5
)
print
(array)
Output :
[[5 2]
[2 5]]
Example #2 :
import
numpy as np
array
=
np.zeros((
3
,
3
),
int
)
np.fill_diagonal(array,
1
)
print
(array)
Output :
[[1 0 0]
[0 1 0]
[0 0 1]]
Similar Reads
- numpy.linalg.eig() Method in Python In NumPy we can compute the eigenvalues and right eigenvectors of a given square array with the help of numpy.linalg.eig(). It will take a square array as a parameter and it will return two values first one is eigenvalues of the array and second is the right eigenvectors of a given square array. Syn 1 min read
- numpy.diag() in Python numpy.diag(a, k=0) : Extracts and construct a diagonal array Parameters : a : array_like k : [int, optional, 0 by default] Diagonal we require; k>0 means diagonal above main diagonal or vice versa. Returns : ndarray Python Code # Python Programming illustrating # numpy.diag method import numpy as 1 min read
- numpy.diagflat() in Python numpy.diagflat (a, k = 0): Create a two-dimensional array with the array_like input as a diagonal to the new output array. Parameters : a : array_like input data with diagonal elements strong>k : [int, optional, 0 by default] Diagonal we require; k>0 means diagonal above main diagonal or vice 1 min read
- numpy.diag_indices() in Python The numpy.diag_indices() function returns indices in order to access the elements of main diagonal of a array with minimum dimension = 2. Returns indices in the form of tuple. to access the main diagonal of an array. Syntax: numpy.diag_indices(n, n_dim = 2) Parameters : n : size of array, for which 2 min read
- numpy.insert() in Python The numpy.insert() function inserts values along the mentioned axis before the given indices. Syntax : numpy.insert(array, object, values, axis = None) Parameters : array : [array_like]Input array. object : [int, array of ints]Sub-array with the index or indices before which values is inserted value 4 min read
- Python sympy | Matrix.diagonalize() method With the help of sympy.Matrix().diagonalize() method, we can diagonalize a matrix. diagonalize() returns a tuple [Tex](P, D)[/Tex], where [Tex]D[/Tex] is diagonal and [Tex]M = PDP^{-1}[/Tex]. Syntax: Matrix().diagonalize() Returns: Returns a tuple of matrix where the second element represents the di 1 min read
- Numpy MaskedArray.filled() method - Python numpy.MaskedArray.filled() function return a copy of self, with masked values filled with a given value. However, if there are no masked values to fill, self will be returned instead as an ndarray. Syntax : numpy.MaskedArray.filled(self, fill_value = None) Parameters : fill_value : [scalar, optional 2 min read
- Python | Numpy MaskedArray.__iand__() numpy.ma.MaskedArray class is a subclass of ndarray designed to manipulate numerical arrays with missing data. With the help of Numpy MaskedArray.__iand__we can get the elements that is anded by the value that is provided as a parameter in the MaskedArray.__iand__() method. Syntax: numpy.MaskedArray 1 min read
- numpy.matrix() in Python This class returns a matrix from a string of data or array-like object. Matrix obtained is a specialised 2D array. Syntax : numpy.matrix(data, dtype = None) : Parameters : data : data needs to be array-like or string dtype : Data type of returned array. Returns : data interpreted as a matrix # Pytho 1 min read
- numpy.full() in Python numpy.full(shape, fill_value, dtype = None, order = 'C') : Return a new array with the same shape and type as a given array filled with a fill_value. Parameters : shape : Number of rows order : C_contiguous or F_contiguous dtype : [optional, float(by Default)] Data type of returned array. fill_value 1 min read