簡體   English   中英

確定 A 是矩陣 B 的子矩陣的索引

[英]Determine indexes where A is a submatrix of matrix B

我在 python 中編碼,我想知道如何獲取矩陣 A 的索引,其中矩陣 B 包含在 A 中。例如,如果我們有

A = [[1,2,3],
     [4,5,6],
     [7,8,9]]

B = [[2,3],
     [5,6]]

然后它返回索引([0,0,1,1], [1,2,1,2]) ,其中第一個列表對應於 x 軸,第二個對應於 y 軸。 或者類似的東西。

謝謝你的幫助!

您可以檢查這個問題來確定一個矩陣是否是另一個矩陣的子矩陣。

然后,您可以利用 NumPy 獲取每個元素的坐標, where function 為:

import numpy as np

A = np.linspace(1, 9, 9).reshape([3, 3])
B = np.asarray([2, 3, 5, 6]).reshape([2, 2])

submatrix_tuple_coord = [list(np.where(A==b)) for bb in B for b in bb]
submatrix_xy = [[int(x), int(y)] for x, y in submatrix_tuple_coord]

# Return a list of list with the row-column indices
submatrix_xy 
>>> [[0, 1], [0, 2], [1, 1], [1, 2]]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM