Find all submatrices of a given matrix
Question:
I need to find all possible sub-matrices of a given matrix mxn. I am trying to do this in python and don’t want to use numpy. Can we do this using loops only?
Eg: 2×2 matrix
Matrix = [
[1, 2],
[3, 4]
]
Submatrices =[ [1],
[1,2],
[2],
[3],
[4],
[3, 4],
[[1], [3]],
[[2],[4]],
[[1,2],[3,4]] ]
Answers:
Assuming the matrix
Matrix = [
[1, 2,3],
[3, 4,5],
[5,6,7]
]
Split into 3 function:
def ContinSubSeq(lst):
size=len(lst)
for start in range(size):
for end in range(start+1,size+1):
yield (start,end)
def getsubmat(mat,start_row,end_row,start_col,end_col):
return [i[start_col:end_col] for i in mat[start_row:end_row] ]
def get_all_sub_mat(mat):
rows = len(mat)
cols = len(mat[0])
for start_row,end_row in ContinSubSeq(list(range(rows))):
for start_col,end_col in ContinSubSeq(list(range(cols))):
yield getsubmat(mat,start_row,end_row,start_col,end_col)
Run this
for i in get_all_sub_mat(Matrix):
print i
Or simpler, put into one function:
def get_all_sub_mat(mat):
rows = len(mat)
cols = len(mat[0])
def ContinSubSeq(lst):
size=len(lst)
for start in range(size):
for end in range(start+1,size+1):
yield (start,end)
for start_row,end_row in ContinSubSeq(list(range(rows))):
for start_col,end_col in ContinSubSeq(list(range(cols))):
yield [i[start_col:end_col] for i in mat[start_row:end_row] ]
I made a function allow to extract matrix from matrix, and i us’it for extract all possible combinaison, you will find the script,
this script solve your problem
def extract(mat, n, n1, m, m1):
l=[]
for i in range(n-1, n1):
r=[]
for j in range(m-1, m1):
if mat[i][j] != []:
r.append(mat[i][j])
l.append(r)
return l
# set 1 in i1 and j1
# set dimension+1 in i2 and j2
res = []
for i1 in range(1, 3):
for i2 in range(1,3):
for j1 in range(1, 3):
for j2 in range(1, 3):
li= extract(mat, i1,i2,j1,j2)
if li !=[] and i2 >= i1 and j2>=j1 :
res.append(li)
print res
def all_sub(r, c, mat): # returns all sub matrices of order r * c in mat
arr_of_subs = []
if (r == len(mat)) and (c == len(mat[0])):
arr_of_subs.append(mat)
return arr_of_subs
for i in range(len(mat) - r + 1):
for j in range(len(mat[0]) - c + 1):
temp_mat = []
for ki in range(i, r + i):
temp_row = []
for kj in range(j, c + j):
temp_row.append(mat[ki][kj])
temp_mat.append(temp_row)
arr_of_subs.append(temp_mat)
return arr_of_subs
Without using functions…
m = [[1,2,3,4],[2,3,4,5],[3,4,5,6]]
r = 3
c = 4
x = 0
while x < r:
y = x+1
while y <= r:
a = 0
while a < c:
b = a+1
while b <= c:
sm = []
for i in m[x:y]:
sm.append(i[a:b])
print(sm)
count += 1
b += 1
a += 1
y += 1
x += 1
def printMatrix(arr,r1,c1,r2,c2):
for i in range(r1,r2+1):
for j in range(c1,c2+1):
print(arr[i][j], end=" ")
print()
def allSubmatrix(arr):
m,n = len(arr), len(arr[0])
for r1 in range(m):
for c1 in range(n):
for r2 in range(r1,m):
for c2 in range(c1,n):
printMatrix(arr,r1,c1,r2,c2) # you can do anything with your submatrix here
print()
I need to find all possible sub-matrices of a given matrix mxn. I am trying to do this in python and don’t want to use numpy. Can we do this using loops only?
Eg: 2×2 matrix
Matrix = [
[1, 2],
[3, 4]
]
Submatrices =[ [1],
[1,2],
[2],
[3],
[4],
[3, 4],
[[1], [3]],
[[2],[4]],
[[1,2],[3,4]] ]
Assuming the matrix
Matrix = [
[1, 2,3],
[3, 4,5],
[5,6,7]
]
Split into 3 function:
def ContinSubSeq(lst):
size=len(lst)
for start in range(size):
for end in range(start+1,size+1):
yield (start,end)
def getsubmat(mat,start_row,end_row,start_col,end_col):
return [i[start_col:end_col] for i in mat[start_row:end_row] ]
def get_all_sub_mat(mat):
rows = len(mat)
cols = len(mat[0])
for start_row,end_row in ContinSubSeq(list(range(rows))):
for start_col,end_col in ContinSubSeq(list(range(cols))):
yield getsubmat(mat,start_row,end_row,start_col,end_col)
Run this
for i in get_all_sub_mat(Matrix):
print i
Or simpler, put into one function:
def get_all_sub_mat(mat):
rows = len(mat)
cols = len(mat[0])
def ContinSubSeq(lst):
size=len(lst)
for start in range(size):
for end in range(start+1,size+1):
yield (start,end)
for start_row,end_row in ContinSubSeq(list(range(rows))):
for start_col,end_col in ContinSubSeq(list(range(cols))):
yield [i[start_col:end_col] for i in mat[start_row:end_row] ]
I made a function allow to extract matrix from matrix, and i us’it for extract all possible combinaison, you will find the script,
this script solve your problem
def extract(mat, n, n1, m, m1):
l=[]
for i in range(n-1, n1):
r=[]
for j in range(m-1, m1):
if mat[i][j] != []:
r.append(mat[i][j])
l.append(r)
return l
# set 1 in i1 and j1
# set dimension+1 in i2 and j2
res = []
for i1 in range(1, 3):
for i2 in range(1,3):
for j1 in range(1, 3):
for j2 in range(1, 3):
li= extract(mat, i1,i2,j1,j2)
if li !=[] and i2 >= i1 and j2>=j1 :
res.append(li)
print res
def all_sub(r, c, mat): # returns all sub matrices of order r * c in mat
arr_of_subs = []
if (r == len(mat)) and (c == len(mat[0])):
arr_of_subs.append(mat)
return arr_of_subs
for i in range(len(mat) - r + 1):
for j in range(len(mat[0]) - c + 1):
temp_mat = []
for ki in range(i, r + i):
temp_row = []
for kj in range(j, c + j):
temp_row.append(mat[ki][kj])
temp_mat.append(temp_row)
arr_of_subs.append(temp_mat)
return arr_of_subs
Without using functions…
m = [[1,2,3,4],[2,3,4,5],[3,4,5,6]]
r = 3
c = 4
x = 0
while x < r:
y = x+1
while y <= r:
a = 0
while a < c:
b = a+1
while b <= c:
sm = []
for i in m[x:y]:
sm.append(i[a:b])
print(sm)
count += 1
b += 1
a += 1
y += 1
x += 1
def printMatrix(arr,r1,c1,r2,c2):
for i in range(r1,r2+1):
for j in range(c1,c2+1):
print(arr[i][j], end=" ")
print()
def allSubmatrix(arr):
m,n = len(arr), len(arr[0])
for r1 in range(m):
for c1 in range(n):
for r2 in range(r1,m):
for c2 in range(c1,n):
printMatrix(arr,r1,c1,r2,c2) # you can do anything with your submatrix here
print()