2016-02-03 16 views
5

Ho bisogno di trovare tutte le possibili sottomatrici di una data matrice mxn. Sto cercando di farlo in python e non voglio usare numpy. Possiamo farlo usando solo i loop?Trova tutte le sottomatrici di una data matrice

Es: matrice 2x2

Matrix = [ 
      [1, 2], 
      [3, 4] 
     ] 

Submatrices =[ [1], 
       [1,2], 
       [2], 
       [3], 
       [4], 
       [3, 4], 
       [[1], [3]], 
       [[2],[4]], 
       [[1,2],[3,4]] ] 
+1

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

+0

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

+0

@ssm Una sezione contigua della matrice dovrebbe essere considerata una corretta sottomatrice. – pankajg

risposta

3

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] ] 
+0

Grazie per gli sforzi, ma la funzione non restituisce tutte le matrici. se provo su [[1, 2], [2, 3]] sta solo ritornando [[1]]. – pankajg

+0

@pankajg Manca uno '+ 1' in' ContinSubSeq', corretto. Provalo ora. – Chiron

+0

@Chiron Questo codice funziona come previsto. Stupefacente. Ma allora puoi spiegare questo in passaggi algoritmici? Sto avendo problemi a capire la resa. – Karthik

0

ho fatto una funzione permette di estrarre da matrice a matrice, e ho us'it per estrarre tutto il possibile combinaison, si trova lo script, questo script risolvere il problema

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 
0
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 
+0

Aggiungi alcune informazioni al tuo codice. – MrLeeh

Problemi correlati