Python:如何选择矩阵值的连续性邻居?

发布于 2025-01-18 03:11:44 字数 1299 浏览 0 评论 0原文

我有一个类似的矩阵:

A = array([[12,  6, 14,  8,  4,  1],
       [18, 13,  8, 10,  9, 19],
       [ 8, 15,  6,  5,  6, 18],
       [ 3,  0,  2, 14, 13, 12],
       [ 4,  4,  5, 19,  0, 14],
       [16,  8,  7,  7, 11,  0],
       [ 3, 11,  2, 19, 11,  5],
       [ 4,  2,  1,  9, 12, 12]])

对于每个单元格,我想在radius of k = 2最近的单元格中选择值。

例如,如果我选择a [3,4]我想要一个像以下功能这样的subsatrix,

array([[18, 13,  8, 10,  9],
       [ 8, 15,  6,  5,  6],
       [ 3,  0,  2, 14, 13],
       [ 4,  4,  5, 19,  0],
       [16,  8,  7,  7, 11]])

我定义了以下功能,

def queen_neighbourhood(Adj, in_row, in_col, k):
    j=k
    k+=1
    neighbourhood = Adj[in_row-j:in_row+k, in_col-j:in_col+k]
    return neighbourhood

例如queen_neighbourhood(a,3,2,2)返回,

array([[18, 13,  8, 10,  9],
       [ 8, 15,  6,  5,  6],
       [ 3,  0,  2, 14, 13],
       [ 4,  4,  5, 19,  0],
       [16,  8,  7,  7, 11]])

​​但是它在边界中不起作用。

例如,对于单元格> [0,0] 我想拥有的

array([[12, 6,  14],
       [18, 13,  8],
       [ 8, 15, 16])

,但它返回queen_neighbourhood(a,0,0,2)

array([], shape=(0, 0), dtype=int64)

I have a matrix like the following:

A = array([[12,  6, 14,  8,  4,  1],
       [18, 13,  8, 10,  9, 19],
       [ 8, 15,  6,  5,  6, 18],
       [ 3,  0,  2, 14, 13, 12],
       [ 4,  4,  5, 19,  0, 14],
       [16,  8,  7,  7, 11,  0],
       [ 3, 11,  2, 19, 11,  5],
       [ 4,  2,  1,  9, 12, 12]])

For each cell I want to select the values in a radius of k=2 closest cells.

For instance if I select the A[3,4] I would like a submatrix like the following

array([[18, 13,  8, 10,  9],
       [ 8, 15,  6,  5,  6],
       [ 3,  0,  2, 14, 13],
       [ 4,  4,  5, 19,  0],
       [16,  8,  7,  7, 11]])

I defined the following function

def queen_neighbourhood(Adj, in_row, in_col, k):
    j=k
    k+=1
    neighbourhood = Adj[in_row-j:in_row+k, in_col-j:in_col+k]
    return neighbourhood

such as queen_neighbourhood(A, 3, 2, 2) returns

array([[18, 13,  8, 10,  9],
       [ 8, 15,  6,  5,  6],
       [ 3,  0,  2, 14, 13],
       [ 4,  4,  5, 19,  0],
       [16,  8,  7,  7, 11]])

However it does not work in borders.

For instance, for the cell [0,0] I would like to have

array([[12, 6,  14],
       [18, 13,  8],
       [ 8, 15, 16])

but it returns queen_neighbourhood(A, 0, 0, 2)

array([], shape=(0, 0), dtype=int64)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟若柳尘 2025-01-25 03:11:44

您可以避免负面指标:

    neighbourhood = Adj[max(in_row-j, 0) : in_row+k,
                        max(in_col-j, 0) : in_col+k]

You could avoid negative indices:

    neighbourhood = Adj[max(in_row-j, 0) : in_row+k,
                        max(in_col-j, 0) : in_col+k]
俏︾媚 2025-01-25 03:11:44

添加到上一个答案;考虑到极端价值观

def queen_neighbourhood(Adj, in_row, in_col, k):
j=k
k+=1
neighbourhood = Adj[max(in_row-j, 0) : min(in_row+k,Adj.shape[0]),
                    max(in_col-j, 0) : min(in_col+k,Adj.shape[1])]
return(neighbourhood)

Adding to the previous answer; taking into consideration the extreme values

def queen_neighbourhood(Adj, in_row, in_col, k):
j=k
k+=1
neighbourhood = Adj[max(in_row-j, 0) : min(in_row+k,Adj.shape[0]),
                    max(in_col-j, 0) : min(in_col+k,Adj.shape[1])]
return(neighbourhood)
錯遇了你 2025-01-25 03:11:44

您可以使用 numpy roll 确保您始终处理中间值,

import numpy as np

def queen_neighbourhood(Adj, in_row, in_col, k):
    j=k
    k+=1
    midrow = int(Adj.shape[0]/2.)+1
    midcol = int(Adj.shape[1]/2.)+1
    Ashift = np.roll(Adj,(in_row-midrow,in_col-midcol),(0,1))
    neighbourhood = Ashift[1:k+1, 1:k+1]
    return neighbourhood


A = np.array([[18, 13,  8, 10,  9],
       [ 8, 15,  6,  5,  6],
       [ 3,  0,  2, 14, 13],
       [ 4,  4,  5, 19,  0],
       [16,  8,  7,  7, 11]])

print(A)
An = queen_neighbourhood(A, 0, 0, 2)
print(An)

这给出了,

[[11 16  8]
 [ 9 18 13]
 [ 6  8 15]]

You can use numpy roll to ensure you are always dealing with the middle value,

import numpy as np

def queen_neighbourhood(Adj, in_row, in_col, k):
    j=k
    k+=1
    midrow = int(Adj.shape[0]/2.)+1
    midcol = int(Adj.shape[1]/2.)+1
    Ashift = np.roll(Adj,(in_row-midrow,in_col-midcol),(0,1))
    neighbourhood = Ashift[1:k+1, 1:k+1]
    return neighbourhood


A = np.array([[18, 13,  8, 10,  9],
       [ 8, 15,  6,  5,  6],
       [ 3,  0,  2, 14, 13],
       [ 4,  4,  5, 19,  0],
       [16,  8,  7,  7, 11]])

print(A)
An = queen_neighbourhood(A, 0, 0, 2)
print(An)

which gives,

[[11 16  8]
 [ 9 18 13]
 [ 6  8 15]]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文