I have a numpy
array in python called my_values
of size 5x5 and a numpy
vector which contains boolean values with size 1x90 (5 False, 85 True) naned cols_indexes. I want to expand my initial array my_values
with zeros in the positions indexes of the cols_indexes that are equal to False
. Thus in the end my transformed matrix my_values
should be of size 5x90 (with 85 new columns filled with zero). A simple example that uses an array instead of a vector of Boolean's is:
def insert_one_per_row(arr, mask, putval):
mask_ext = np.column_stack((mask, np.zeros((len(mask), 1), dtype=bool)))
out = np.empty(mask_ext.shape, dtype=arr.dtype)
out[~mask_ext] = arr.ravel()
out[mask_ext] = putval
return out
y = np.arange(25).reshape(5, 5)
x = np.array([[False, True, False, False, False],
[False, True, False, False, False],
[False, True, False, False, False],
[False, True, False, False, False],
[False, True, False, False, False]], dtype=bool)
arr = insert_one_per_row(y, x, putval=0)
This example works with an array of Boolean's. However in my case x
is a vector instead of an array. x
contains True
for a new column in the position that I need to add and False
for an existing one in the position of the final array. How can I insert the new columns using the vector x
instead of the matrix x
?