如何访问python中矩阵每个元素的相邻单元格?
How to access the adjacent cells of each elements of matrix in python?
在这里,如果两个单元格共享一个边界,则它们被认为是相邻的。
例如:
A = 5 6 4
2 1 3
7 9 8
此处索引 0,0 的相邻元素位于索引 [0,1] 和 [1,0],而对于索引 1,1,相邻元素位于索引 [0,1],[1,0] ,[2,1] 和 [1,2].
假设你有m
xn
矩阵,你想找到单元格(i
,j
)的相邻索引:
def get_adjacent_indices(i, j, m, n):
adjacent_indices = []
if i > 0:
adjacent_indices.append((i-1,j))
if i+1 < m:
adjacent_indices.append((i+1,j))
if j > 0:
adjacent_indices.append((i,j-1))
if j+1 < n:
adjacent_indices.append((i,j+1))
return adjacent_indices
为了检查对角线,关于 Casper Dijkstrao 问的问题,我通常会写这样的代码:
def adj_finder(matrix, position):
adj = []
for dx in range(-1, 2):
for dy in range(-1, 2):
rangeX = range(0, matrix.shape[0]) # X bounds
rangeY = range(0, matrix.shape[1]) # Y bounds
(newX, newY) = (position[0]+dx, position[1]+dy) # adjacent cell
if (newX in rangeX) and (newY in rangeY) and (dx, dy) != (0, 0):
adj.append((newX, newY))
return adj
The function gets the matrix
argument to extract the size of its row and column (I use numpy
, so matrix.shape
returns (row_size, column_size)
tuple).
It also gets the current cell as pointer
argument (it's like (X,Y)
).
Then It generate adjacent cells, and if they were legit (1. they were not out of the bound, and 2. not identical to reference position), it adds them to adjacent list, adj
.
我想强调的是,使用上述算法,您也可以轻松获得更远距离的邻居。只需修改 for 循环中的范围,如下所示:
for v in range(0-distance, 1+distance):
for h in range(0-distance, 1+distance):
...
Where distance
is the maximum distance of adjacent you want to let in.
这将是另一种方式 - 概率。涉及一些数学技巧或Pythonic
方式:
def neighbours(grid, r, c):
vals = sum((row[c -(c>0): c+2]
for row in grid[r -(r>0):r+2]), [])
vals.remove(grid[r][c]) # rm itself.
return vals
grid = [[1, 5, 4, 9],
[2, 8, 3, 8],
[6, 3, 6, 3],
[7, 4, 7, 1]]
输出:(所有项目都按顺序排列)
print(f' {neighbours(grid, 2, 2)} ') # [8, 3, 8, 3, 3, 4, 7, 1]
print(f' {neighbours(grid, 0, 0)} ') # [5, 2, 8]
print(f' {neighbours(grid, 1, 1)} ') # [[1, 5, 4, 2, 3, 6, 3, 6]
在这里,如果两个单元格共享一个边界,则它们被认为是相邻的。 例如:
A = 5 6 4
2 1 3
7 9 8
此处索引 0,0 的相邻元素位于索引 [0,1] 和 [1,0],而对于索引 1,1,相邻元素位于索引 [0,1],[1,0] ,[2,1] 和 [1,2].
假设你有m
xn
矩阵,你想找到单元格(i
,j
)的相邻索引:
def get_adjacent_indices(i, j, m, n):
adjacent_indices = []
if i > 0:
adjacent_indices.append((i-1,j))
if i+1 < m:
adjacent_indices.append((i+1,j))
if j > 0:
adjacent_indices.append((i,j-1))
if j+1 < n:
adjacent_indices.append((i,j+1))
return adjacent_indices
为了检查对角线,关于 Casper Dijkstrao 问的问题,我通常会写这样的代码:
def adj_finder(matrix, position):
adj = []
for dx in range(-1, 2):
for dy in range(-1, 2):
rangeX = range(0, matrix.shape[0]) # X bounds
rangeY = range(0, matrix.shape[1]) # Y bounds
(newX, newY) = (position[0]+dx, position[1]+dy) # adjacent cell
if (newX in rangeX) and (newY in rangeY) and (dx, dy) != (0, 0):
adj.append((newX, newY))
return adj
The function gets the
matrix
argument to extract the size of its row and column (I usenumpy
, somatrix.shape
returns(row_size, column_size)
tuple).It also gets the current cell as
pointer
argument (it's like(X,Y)
).Then It generate adjacent cells, and if they were legit (1. they were not out of the bound, and 2. not identical to reference position), it adds them to adjacent list,
adj
.
我想强调的是,使用上述算法,您也可以轻松获得更远距离的邻居。只需修改 for 循环中的范围,如下所示:
for v in range(0-distance, 1+distance):
for h in range(0-distance, 1+distance):
...
Where
distance
is the maximum distance of adjacent you want to let in.
这将是另一种方式 - 概率。涉及一些数学技巧或Pythonic
方式:
def neighbours(grid, r, c):
vals = sum((row[c -(c>0): c+2]
for row in grid[r -(r>0):r+2]), [])
vals.remove(grid[r][c]) # rm itself.
return vals
grid = [[1, 5, 4, 9],
[2, 8, 3, 8],
[6, 3, 6, 3],
[7, 4, 7, 1]]
输出:(所有项目都按顺序排列)
print(f' {neighbours(grid, 2, 2)} ') # [8, 3, 8, 3, 3, 4, 7, 1]
print(f' {neighbours(grid, 0, 0)} ') # [5, 2, 8]
print(f' {neighbours(grid, 1, 1)} ') # [[1, 5, 4, 2, 3, 6, 3, 6]