I'm trying to do something along the lines of a masked broadcast, where only certain values are broadcasted.
Suppose I have one bigger array, bigger_array, and one smaller array, smaller_array:
import numpy as np
import numpy.ma as ma
bigger_array = np.zeros((4,4), dtype=np.int32)
smaller_array = np.ones((2,2), dtype=np.int32)
Now, I only want the first three values of the smaller array to replace those of a certain section of the bigger array, but masking doesn't do what I'd hoped it would do:
masked_smaller_array = ma.masked_array(smaller_array, mask=[(0, 0), (0, 1)])
bigger_array[2:4, 2:4] = masked_smaller_array
This just returns the same thing a regular broadcast would, namely:
[[0 0 0 0]
[0 0 0 0]
[0 0 1 1]
[0 0 1 1]]
Instead of my hoped for
[[0 0 0 0]
[0 0 0 0]
[0 0 1 1]
[0 0 1 0]]
Stripping out the masked value before overriding via
bigger_array[2:4, 2:4] = masked_smaller_array[~masked_smaller_array.mask]
is also of no use as that flattens the array making broadcasting incompatible.
Is there some other way to achieve the same effect perhaps?