Supponendo matrice
Matrix = [
[1, 2,3],
[3, 4,5],
[5,6,7]
]
Split in funzione 3:
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)
Eseguire questa
for i in get_all_sub_mat(Matrix):
print i
O più semplice, mettere in una sola funzione:
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] ]
Che dire di una matrice 3x3? La rimozione di un gruppo di colonne casuali è ancora considerata una sottomatrice? O una sezione contigua della matrice dovrebbe essere considerata una sottomatrice appropriata? – ssm
Penso che sia necessario implementare due funzioni: 'Combination (x, n)' e 'SubMatrix (x_start, x_end, y_start, y_end)'. E dovrebbe essere risolto – Chiron
@ssm Una sezione contigua della matrice dovrebbe essere considerata una corretta sottomatrice. – pankajg