changing the values of the diagonal of a matrix in numpy

前端 未结 7 900
孤城傲影
孤城傲影 2020-12-08 19:20

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

7条回答
  •  一个人的身影
    2020-12-08 20:03

    >>> a = numpy.random.rand(2,2)
    >>> a
    array([[ 0.41668355,  0.07982691], 
           [ 0.60790982,  0.0314224 ]])
    >>> a - numpy.diag(numpy.diag(a))
    array([[ 0.        ,  0.07982691],
           [ 0.60790982,  0.        ]])
    

提交回复
热议问题