Update Numpy Array Where Not Masked
My question is twofolded First, lets say I've two numpy arrays, that are partially masked array_old [[-- -- -- --] [10 11 -- --] [12 14 -- --] [-- -- 17 --]] array_update [[--
Solution 1:
Use a[~b.mask] = b.compressed()
.
a[~b.mask]
selects all the values in a
where b
is not masked. b.compressed()
is a flattened array with all the non-masked values in b
.
Example:
>>> a = np.ma.masked_equal([[0, 0, 0, 0], [10, 11, 0, 0], [12, 14, 0, 0], [0, 0, 17, 0]], 0)
>>> b = np.ma.masked_equal([[0, 5, 0, 0], [0, 0, 9, 0], [0, 15, 8, 13], [0, 0, 19, 16]], 0)
>>> a[~b.mask] = b.compressed()
>>> a
[[-- 5 -- --]
[10 11 9 --]
[12 15 8 13]
[-- -- 19 16]]
This should work with 3d arrays too.
Post a Comment for "Update Numpy Array Where Not Masked"