Determine indexes where A is a submatrix of matrix B

Question:

I’m coding in python and I would like to know how I can get the indexes of a matrix A where the matrix B is contained in A. For example, if we have

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

and

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

Then it returns indexes ([0,0,1,1], [1,2,1,2]), where the first list corresponds to x-axis, and the second to the y-axis. Or something like this.

Thank you for yor help !

Asked By: salomepx

||

Answers:

You can check this question to find if a matrix is a submatrix of another one.

Then, you can get the coordinates of each element exploiting the NumPy where function as:

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]]
Answered By: Morgana