假設矩陣
Matrix = [
[1, 2,3],
[3, 4,5],
[5,6,7]
]
分成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)
運行該
for i in get_all_sub_mat(Matrix):
print i
或者更簡單,投入一個功能:
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] ]
怎麼樣一個3x3矩陣?刪除一組仍然視爲子矩陣的隨機列?或者矩陣的連續部分應該被認爲是合適的子矩陣? – ssm
我想你需要實現兩個函數:'Combination(x,n)'和'SubMatrix(x_start,x_end,y_start,y_end)'。它應該被解決 – Chiron
@ssm矩陣的連續部分應該被認爲是一個合適的子矩陣。 – pankajg