Python: Insert columns into a numpy array based on mask
Here's one way to do it in two steps:
(i) Create an array of zeros of the correct shape (the first dimension of ip_array
and the second dimension of mask
)
(ii) Use the mask
across the second dimension (as a boolean mask) and assign the values of ip_array
to the array of zeros.
out = np.zeros((ip_array.shape[0], mask.shape[1])).astype(int)
out[..., mask[0].astype(bool)] = ip_array
print(out)
Output:
[[0 4 5 0 2]
[0 3 2 0 1]
[0 1 8 0 6]]