When I have an array a and a boolean mask b, I can find the ‘masked’ vector c.
a = np.array([1, 2, 4, 7, 9])
b = np.array([True, False, True, True, False])
c = a[b]
Now suppose, it’s the other way around. I have c and b and would like to arrive at d (below). What is the easiest way to do this?
c = np.array([1, 4, 7])
b = np.array([True, False, True, True, False])
d = np.array([1, 0, 4, 7, 0])
>Solution :
You could use:
d = np.zeros_like(b, dtype=c.dtype)
d[b] = c
Output: array([1, 0, 4, 7, 0])