I would like to change a range of values in a 2d NumPy array in a simple way. For example, imagine I have a 2d array of shape (5,5). If I want to change the values of the upper 2x2 'block', I can use the NumPy put function. This is an example:
import numpy as np
A = np.zeros(shape=(5,5))
np.put(A, [[0,1,2,5,6,7,10,11,12]], 1)
Resulting in:
print(A)
[[1. 1. 1. 0. 0.]
[1. 1. 1. 0. 0.]
[1. 1. 1. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]
Ideally, however, I would like to do the following: use the coordinates of a square to directly change a range of values, without flattening the array. In the example, these coordinates would be [(0,0), (2,2)]. Is there a Pythonic way of doing this?