在numpy数组中的已知索引(x,y)周围切片20×20的区域 [英] Slicing a 20×20 area around known indices (x, y) in a numpy array
本文介绍了在numpy数组中的已知索引(x,y)周围切片20×20的区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个大型的2D numpy数组,我知道一对代表该数组一个元素的索引.我想将此元素和周围的20×20区域设置为零;我尝试使用切片技术:
I have a large 2D numpy array for which I know a pair of indices which represent one element of the array. I want to set this element and the surrounding 20×20 area equal to zero; I have attempted using a slicing technique:
s = array[x:10, y:10]
s == 0
但是,尽管先前已经定义了x和y,但这是一个无效的切片".
However, although x and y are previously defined, this is an 'invalid slice'.
对于我是Python的新手,我会提供有关如何完成此操作的任何建议.
I would appreciate any suggestions as to how I can accomplish this as I am new to Python.
推荐答案
my_array[x - 10:x + 10, y - 10:y + 10] = 0
或
s = my_array[x - 10:x + 10, y - 10:y + 10]
s[:] = 0
这篇关于在numpy数组中的已知索引(x,y)周围切片20×20的区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文