Python – changing the values of the diagonal of a matrix in numpy

numpypython

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.

Best Solution

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