Setting values in a numpy arrays indexed by a slice and two boolean arrays

后端 未结 1 1560
孤独总比滥情好
孤独总比滥情好 2021-01-14 06:26

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 ce

相关标签:
1条回答
  • 2021-01-14 06:53

    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
    
    0 讨论(0)
提交回复
热议问题