Skip to content Skip to sidebar Skip to footer

Setting Values In A Numpy Arrays Indexed By A Slice And Two Boolean Arrays

I have two numpy arrays: a = np.arange(100*100).reshape(100,100) b = np.random.rand(100, 100) I also have a tuple of slices to extract a certain piece of the array: slice_ = (slic

Solution 1:

Slicing a numpy array returns a view, but boolean indexing returns a copy of an array. So when you indexed it first time with boolean index in a[slice_][indices][high_indices], you got back a copy, and the value 42 is assigned to a copy and not to the array a. You can solve the problem by chaining the boolean index:

a[slice_][(a[slice_] > 700) & (b[slice_] > 0.5)] = 42

Post a Comment for "Setting Values In A Numpy Arrays Indexed By A Slice And Two Boolean Arrays"