2016-03-07 61 views
1

從以前的文章中,我學會了如何改變一個非常快速和簡單的方法在一個二維數組中的所有值數組值:操縱一個簡單的方法

例如設定爲小於255 X的所有值:

arr[arr < 255] = x

現在我想要做類似的事情,但不只是將值設置爲x。

我想在特定的方式處理該數組中的值,當它低於閾值時。

arr[abs(arr) < threshold] = arr(*where condition is true*) - threshold*x

ARR - theshold * X不起作用,因爲ARR是完整的陣列。

我認爲需要使用與np.where的循環來解決這個問題,這與以前不太一樣。有沒有更好的方法?

回答

2

這應該是很容易擴展你已經做了什麼:

x = random.rand(100) 
threshold = 0.2 
indices = abs(x) < threshold 
x[indices] = x[indices] - threshold*x[indices]