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
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.
To convert the shape of a NumPy array ndarray , use the reshape() method of ndarray or the numpy.
If X
is your array and c
is the factor,
X[np.diag_indices_from(X)] /= c
See diag_indices_from
in the Numpy manual.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With