找到Python中基质子集的重新发生

发布于 2025-01-22 04:48:31 字数 471 浏览 0 评论 0原文

我有一个场景,其中矩阵输入为5*5,

4 4 4 1 1
4 4 4 0 2
4 4 4 5 1
4 4 4 2 3
4 4 4 4 4 

如果我给出任何输入,例如2 3或3 3,则应找到矩阵的重新出现。 一样

像3 * 3

,它在第1行中具有这样的矩阵子集

; -

4 4 4 
4 4 4
4 4 4

同样,如果我指定3 * 3,则矩阵的子集有3个出现。是否可以将其转换为Python?

I have a scenario where the matrix input is, 5*5

4 4 4 1 1
4 4 4 0 2
4 4 4 5 1
4 4 4 2 3
4 4 4 4 4 

If I give any input like 23 or 33, it should find the reoccurrences of the matrix. like

3*3

it has the subsets of matrix like this

from row 1;-

4 4 4 
4 4 4
4 4 4

likewise, If I specify 3 * 3 the subset of the matrix has 3 occurrences. Is it possible to convert this to Python?

Reoccurrence image

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

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

发布评论

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

评论(1

溺孤伤于心 2025-01-29 04:48:31

假设您的原始5x5矩阵称为input_matrix。您可以做到这一点,以从上左上重复序列中找到给定的子序列的次数:

input_size = input_matrix.shape[0]

# However many rows and columns the sub-matrix should have.
num_rows = 3
num_cols = 3

# The sub-matrix to find the number of occurrences of.
orig_matrix = input_matrix[:num_rows, :num_cols]

num_occurrences = 0

# Iterate through all indices at positions where the sub-matrix could still fit 
# inside the input matrix.
for row_num in range(input_size - num_rows + 1):
    for col_num in range(input_size - num_cols + 1):

      # Get the sub-matrix at those indices
      this_matrix = input_matrix[row_num:row_num + num_rows, col_num:col_num + num_cols]

      # Get the difference between the original sub-matrix and the current submatrix.
      # If difference at all points is 0, the submatrices are the same.
      if np.count_nonzero(orig_matrix - this_matrix, axis=None) == 0:
          num_occurrences += 1

num_occurrences包含您想要的数字。我敢肯定,有更有效的方法可以做到这一点,但这就是我所拥有的。

Say your original 5x5 matrix is called input_matrix. You could do this to find the number of times a given submatrix from top-left repeats:

input_size = input_matrix.shape[0]

# However many rows and columns the sub-matrix should have.
num_rows = 3
num_cols = 3

# The sub-matrix to find the number of occurrences of.
orig_matrix = input_matrix[:num_rows, :num_cols]

num_occurrences = 0

# Iterate through all indices at positions where the sub-matrix could still fit 
# inside the input matrix.
for row_num in range(input_size - num_rows + 1):
    for col_num in range(input_size - num_cols + 1):

      # Get the sub-matrix at those indices
      this_matrix = input_matrix[row_num:row_num + num_rows, col_num:col_num + num_cols]

      # Get the difference between the original sub-matrix and the current submatrix.
      # If difference at all points is 0, the submatrices are the same.
      if np.count_nonzero(orig_matrix - this_matrix, axis=None) == 0:
          num_occurrences += 1

num_occurrences contains the number you want. I'm sure there are more efficient ways to do this, but here's what I've got.

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