简体   繁体   English

如何切割2D numpy数组以获得其直接邻居?

[英]How to slice a 2D numpy array to get its direct neighbors?

I want to loop through my 2D numpy array and check all its direct neighbors. 我想遍历我的2D numpy数组并检查它的所有直接邻居。 If I make a numpy array like so: 如果我像这样制作一个numpy数组:

tilemap = np.arange(16).reshape(4,4)

It will look something like this: 它看起来像这样:

 [[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]

The loop that I created to help me find the neighbors for each spot in the array looks like this: 我创建的循环可以帮助我找到数组中每个点的邻居,如下所示:

 import numpy as np

 mapwidth = 4
 mapheight = 4

 tilemap = np.arange(mapwidth * mapheight).reshape(mapwidth, mapheight)

 row = 0
 for i in tilemap:
     count = 0
     for j in i:
         column = j % mapwidth
         check = tilemap[row-1:row+2, column-1:column+2]
         print(check)
         count += 1
         if count % mapheight == 0:
             row += 1

However, when I do this, I will not find any neighbors for the spot in the array with the values 0, 1, 2, 3, 4, 8, and 12. I understand why this is the case. 但是,当我这样做时,我不会在数组中找到任何邻居,其值为0,1,2,3,4,8和12.我明白为什么会这样。 for example, if I take the value of 8. it has the indices of [2,0]. 例如,如果我取值8.它的索引为[2,0]。 the row-1 will result in -1, which is the same as index 3 in this example. row-1将导致-1,这与此示例中的索引3相同。 the row+2 is 2. And slicing 2:3 will result in nothing because there is nothing between 2 and 3. 行+ 2是2.而切片2:3将不会产生任何结果,因为2和3之间没有任何内容。

Anyway, the result I'm looking for is something like this (for value 8): 无论如何,我正在寻找的结果是这样的(价值8):

[[4  5]
[ 8  9]
[12 13]]

I know I can achieve this by piling some if-statements but I wonder if there is a more elegant way of dealing with this. 我知道我可以通过堆积一些if语句来实现这一点,但我想知道是否有更优雅的方法来处理这个问题。

Thanks for your time. 谢谢你的时间。

(For those who are curious to know this): The neighbors for eg value 11 actually return like I want them to, without any errors. (对于那些很想知道这一点的人):例如值11的邻居实际上就像我想要的那样返回,没有任何错误。 It returns this: 它返回:

[[6  7]
[10 11]
[14 15]]

EDIT: 编辑:

I should also mention I tried this: 我还要提一下我试过这个:

check = np.take(tilemap, tilemap[row-1:row+2, column-1:column+2], mode = 'clip')

But this did not work. 但这没效果。

You can simplify the way your loop is written and not assume as much about the contents of the array, making your code more flexible. 您可以简化循环的编写方式,而不是假设数组的内容,使代码更灵活。 Numpy has an nditer class that can be used to iterate over an array. Numpy有一个nditer类,可用于迭代数组。 You can also use it to get the multi-dimensional index of each element. 您还可以使用它来获取每个元素的多维索引 Iteration can be further simplified using the ndenumerate class, similar to Pythons builtin enumerate . 使用ndenumerate类可以进一步简化迭代,类似于Pythons builtin enumerate If you do not need to get the elements back, just the index, you can use ndindex . 如果您不需要返回元素,只需索引,就可以使用ndindex Here is an example using ndindex : 以下是使用ndindex的示例:

for r, c in ndindex(tilemap.shape):
    check = tilemap[max(r-1, 0):min(r+1, mapheight), max(c-1, 0):min(c+1, mapwidth)]
    print(check)

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

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