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
A quick way to access the diagonal of a square (n,n) numpy array is with arr.flat[::n+1]:
(n,n)
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