Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing the values of the diagonal of a matrix in numpy

Tags:

python

numpy

how can I change the values of the diagonal of a matrix in numpy?

I checked Numpy modify ndarray diagonal, but the function there is not implemented in numpy v 1.3.0.

lets say we have a np.array X and I want to set all values of the diagonal to 0.

like image 945
pacodelumberg Avatar asked Mar 31 '12 18:03

pacodelumberg


People also ask

How do you change the diagonal elements of a matrix in NumPy?

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. Return : Return the filled value in the diagonal of an array.

How do you extract the diagonal elements of a matrix?

Description. D = diag( v ) returns a square diagonal matrix with the elements of vector v on the main diagonal. D = diag( v , k ) places the elements of vector v on the k th diagonal. k=0 represents the main diagonal, k>0 is above the main diagonal, and k<0 is below the main diagonal.


1 Answers

Did you try numpy.fill_diagonal? See the following answer and this discussion. Or the following from the documentation (although currently broken):

http://docs.scipy.org/doc/numpy/reference/generated/numpy.fill_diagonal.html

like image 108
Arjun Avatar answered Oct 12 '22 01:10

Arjun