查找给定矩阵的所有子矩阵

时间:2016-02-03 08:15:51

标签: python matrix

我需要找到给定矩阵mxn的所有可能的子矩阵。我试图在python中这样做,不想使用numpy。我们可以只使用循环吗?

例如:2x2矩阵

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

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

4 个答案:

答案 0 :(得分:3)

假设矩阵

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

答案 1 :(得分:0)

我做了一个函数允许从矩阵中提取矩阵,而我们用于提取所有可能的组合,你会发现脚本,  这个脚本解决了你的问题

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

答案 2 :(得分: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

答案 3 :(得分:0)

不使用功能...

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