简体   繁体   English

轻松操作数组值

[英]Manipulate array values in an easy way

From a previous post, I learned how to change all values in a 2D-Array in a very fast and easy way: 在上一篇文章中,我学习了如何以一种非常快速简便的方式来更改2D数组中的所有值:

eg set all values smaller than 255 to x: 例如,将所有小于255的值设置为x:

arr[arr < 255] = x

Now I want to do something similar, but not just setting the value to x. 现在,我想做类似的事情,而不仅仅是将值设置为x。

I want to manipulate the value in this array in a specific way when it's below a treshold. 当它低于阈值时,我想以特定方式操纵此数组中的值。

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

arr - theshold*x does not work, since arr is the complete array. arr-theshold * x不起作用,因为arr是完整的数组。

I think that a loop with np.where is needed to solve this problem which isn't quite easy as before. 我认为需要使用np.where循环来解决此问题,这并不像以前那样容易。 Is there any better way? 有什么更好的办法吗?

It should be pretty easy to extend what you've already done: 扩展已经完成的工作应该很容易:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM