I have a numpy array with zeros and non-zeros and shape (10,10). To a subpart of this array I need to add a certain value, where initial value is not zero.
a[2:7,2:7] += 0.5 #But with a condition that a[a!=0]
Currently, I do it in a rather cumbersome way, by first making a copy of the array and modifying the second array consistently and then copying back to the first.
b = a.copy()
b[b!=0] = 1
b[2:7,2:7] *= 0.5
b[b ==1] =0
a += b
Is there more elegant way to achieve this?
b = a[2:7,2:7]and thenb[b!=0] += 0.5. Test it withprint(a).