I have 2 arrays:
- image
is a NxN array,
- indices
is a Mx2 array, where the last dimension stores valid indices into image
.
I want to add 1 in image
for each occurrence of that index in indices
.
It seems like numpy.add.at(image, indices, 1)
should do the trick, except that I can't get it to perform 2-d indexing into the image
:
image = np.zeros((5,5), dtype=np.int32)
indices = np.array([[1,1], [1,1], [3,3]])
np.add.at(image, indices, 1)
print(image)
Result:
[[0 0 0 0 0]
[4 4 4 4 4]
[0 0 0 0 0]
[2 2 2 2 2]
[0 0 0 0 0]]
Desired result:
[[0 0 0 0 0]
[0 2 0 0 0]
[0 0 0 0 0]
[0 0 0 1 0]
[0 0 0 0 0]]