在python中得到相邻的点矩阵

时间:2013-02-06 04:18:09

标签: python matrix point

如果在2D,p(x,y),我想要一个3 * 3的相邻矩阵:

(x-1,y-1), (x,y-1), (x+1,y-1),
...
(x-1,y+1), (x,y+1), (x+1,y+1),

如果是3D(3 * 3 * 3),4D(3 * 3 * 3 * 3),......?

有更好的功能吗?

2 个答案:

答案 0 :(得分:2)

您也可以使用itertools.product,具体取决于您喜欢的输出格式。它会比numpy方法慢,但我发现它更容易理解:

from itertools import product

def adjacent_grid(centre):
    steps = product([-1, 0, 1], repeat=len(centre))
    return (tuple(c+d for c,d in zip(centre, delta)) for delta in steps)

给出了

>>> list(adjacent_grid((3,)))
[(2,), (3,), (4,)]
>>> list(adjacent_grid((3,3)))
[(2, 2), (2, 3), (2, 4), (3, 2), (3, 3), (3, 4), (4, 2), (4, 3), (4, 4)]
>>> list(adjacent_grid((3,3,3)))
[(2, 2, 2), (2, 2, 3), (2, 2, 4), (2, 3, 2), (2, 3, 3), (2, 3, 4), (2, 4, 2), (2, 4, 3), (2, 4, 4), (3, 2, 2), (3, 2, 3), (3, 2, 4), (3, 3, 2), (3, 3, 3), (3, 3, 4), (3, 4, 2), (3, 4, 3), (3, 4, 4), (4, 2, 2), (4, 2, 3), (4, 2, 4), (4, 3, 2), (4, 3, 3), (4, 3, 4), (4, 4, 2), (4, 4, 3), (4, 4, 4)]

答案 1 :(得分:1)

您可以使用numpy中的广播来获得结果:

import numpy as np
def p(*args):
    args = np.array(args)
    idx = np.array([-1, 0, 1])
    a = np.broadcast_arrays(*np.ix_(*(args[:,None] + idx)))
    return np.concatenate([x[..., None] for x in a], axis=-1)

结果形状为2D中的(3,3,2),3D中的(3,3,3,3),4D中的(3,3,3,3,4):

>>> p(3, 8)
array([[[2, 7],
        [2, 8],
        [2, 9]],

       [[3, 7],
        [3, 8],
        [3, 9]],

       [[4, 7],
        [4, 8],
        [4, 9]]])