I've tried to find a neat solution to this, but I'm slicing several 2D arrays of the same shape in the same manner. I've tidied it up as much as I can by defining a list containing the 'x,y' center e.g. cpix = [161, 134]
What I'd like to do is instead of having to write out the slice three times like so:
我试图找到一个简洁的解决方案,但我正在以相同的方式切割几个相同形状的2D阵列。通过定义包含'x,y'中心的列表,我尽可能地整理了它,例如cpix = [161,134]我想做的是不必像这样写三次切片:
a1 = array1[cpix[1]-50:cpix[1]+50, cpix[0]-50:cpix[0]+50]
a2 = array2[cpix[1]-50:cpix[1]+50, cpix[0]-50:cpix[0]+50]
a3 = array3[cpix[1]-50:cpix[1]+50, cpix[0]-50:cpix[0]+50]
is just have something predefined (like maybe a mask?) so I can just do a
只是有预定义的东西(比如可能是面具?)所以我可以做一个
a1 = array1[predefined_2dslice]
a2 = array2[predefined_2dslice]
a3 = array3[predefined_2dslice]
Is this something that numpy supports?
这是numpy支持的东西吗?
1 个解决方案
#1
11
Yes you can use numpy.s_
:
是的你可以使用numpy.s_:
Example:
例:
>>> a = np.arange(10).reshape(2, 5)
>>>
>>> m = np.s_[0:2, 3:4]
>>>
>>> a[m]
array([[3],
[8]])
And in this case:
在这种情况下:
my_slice = np.s_[cpix[1]-50:cpix[1]+50, cpix[0]-50:cpix[0]+50]
a1 = array1[my_slice]
a2 = array2[my_slice]
a3 = array3[my_slice]
You can also use numpy.r_
in order to translates slice objects to concatenation along the first axis.
您还可以使用numpy.r_将片对象转换为沿第一轴的连接。
#1
11
Yes you can use numpy.s_
:
是的你可以使用numpy.s_:
Example:
例:
>>> a = np.arange(10).reshape(2, 5)
>>>
>>> m = np.s_[0:2, 3:4]
>>>
>>> a[m]
array([[3],
[8]])
And in this case:
在这种情况下:
my_slice = np.s_[cpix[1]-50:cpix[1]+50, cpix[0]-50:cpix[0]+50]
a1 = array1[my_slice]
a2 = array2[my_slice]
a3 = array3[my_slice]
You can also use numpy.r_
in order to translates slice objects to concatenation along the first axis.
您还可以使用numpy.r_将片对象转换为沿第一轴的连接。