numpy-更新数组中的所有值,除了切片
我有以下数组:
arr = np.array([[1, 1, 1, 1, 1, 1],
[2, 0, 0, 0, 0, 2],
[3, 0, 0, 0, 0, 3],
[4, 4, 4, 4, 4, 4]])
inverse_slice = arr[1:3, 1:5]
我想更新数组中所有值的值,切片中的值除外。 例如,这可能是将所有值乘以 2,切片中的值除外(在本例中为 0 的矩形)。
我怎样才能有效地实现这一目标?
注意:性能至关重要,因为我正在处理的实际数组非常大,因此使用 Python for 循环进行迭代是不够的。
I have the following array:
arr = np.array([[1, 1, 1, 1, 1, 1],
[2, 0, 0, 0, 0, 2],
[3, 0, 0, 0, 0, 3],
[4, 4, 4, 4, 4, 4]])
inverse_slice = arr[1:3, 1:5]
I want to update the values of all values in the array, execpt for the values in the slice.
For example, this could be multiplying all values with 2, exepct for values in the slice (the rectangle of 0's in this case).
How can I achieve this in an efficient manner?
NOTE: Performance is critical, as the actual array I'm processing is very large, so iteration using Python for-loops is not sufficient.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不是这个?
arr2:
Why not this?
arr2
:您可以通过块来完成此操作。
可能不是最优雅的方法,尽管您可以在需要影响的确切单元格上循环,同时根据未触及的矩形的大小保持小于O(n^2)的复杂性:
You could do this working by chunks.
Probably not the most elegant way to do it, though you can cycle over the exact cells you need to affect, while keeping a less than O(n^2) complexity depending on the size of the untouched rectangle: