I have a numpy array created with np.zeros((10,10)).
There are 1-s randomly in the array e.g.:
arr[2][4] = 1
arr[3][5] = 1
arr[4][6] = 1
resulting in:
[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
I am given a position in the array e.g.: arr[2][4].
How do I extract both diagonals(top left to bottom right and top right to bottom left) using Numpy methods, taking bounds into calculation.
Here is a complete example of what I want to achive:
import numpy as np
arr = np.zeros((10,10))
arr[2][4] = 1
arr[3][5] = 1
arr[4][6] = 1
def extract_diags(arr, position):
# some numpy magic here
return diags
topl_bottomr_diag, topr_bottoml_diag = extract_diags(arr, (2,4))
print(topl_bottomr_diag)
>>> (0, 0, 1, 1, 1, 0, 0, 0)
print(topr_bottoml_diag)
>>> (0, 0, 1, 0, 0, 0, 0)