Just ran into this fun problem but I can't figure it out correctly
You have a matrix:
| 1 2 3 |
| 4 5 6 |
| 7 8 9 |
and your given two coordinates:
(1,1) and (2,2)
find the corresponding submatrix:
so (1,1)
and (2,2)
will create
|5 6|
|8 9|
my code:
def get_submatrix(matrix, coor):
m = len(matrix)
#chooses what rows
lstart = coor[0][0]
lend = coor[1][0]
#chooses what columns
estart = coor[0][1]
eend = coor[1][1]
subgrid = []
print('coors: (%s, %s), (%s, %s) ' % (lstart, estart, lend, eend))
#iterate through the matrices
for i in range(lstart,lend+1):
# print('matrix index: %s' % i)
print('matrix: %s' % matrix[i])
row = matrix[i]
subgrid.append(row[estart:len(row)])
print('subgrid: %s' % subgrid)
return subgrid
My code uses len(row)
to pull the end of the row. which might work for some cases but not all cases.
Any help on crafting the for loop to obtain the submatrix?