Numpy modify ndarray diagonal

后端 未结 4 2018
轻奢々
轻奢々 2020-12-06 05:11

is there any way in numpy to get a reference to the array diagonal? I want my array diagonal to be divided by a certain factor Thanks

4条回答
  •  情歌与酒
    2020-12-06 05:47

    A quick way to access the diagonal of a square (n,n) numpy array is with arr.flat[::n+1]:

    n = 1000
    c = 20
    a = np.random.rand(n,n)
    
    a[np.diag_indices_from(a)] /= c # 119 microseconds
    a.flat[::n+1] /= c # 25.3 microseconds
    

提交回复
热议问题