Using NumPy and according a matrix is equal to an array … I want to shrink a matrix. I mean by that remove all the borders empty lines (filled with 0) Example: Matrix to shrink: array([[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8) Result expected: array([[1, 0, 1], [0, 0, 0], [1, 0, 1]], dtype=uint8) To do that, I wrote this function: import numpy as np def shrink(mat): while mat[0].any()==0 or mat[-1].any()==0 or mat[:,0].any()==0 or mat[:,-1].any()==0: if mat[0].any()==0: mat = np.delete(mat, 0, 0) if mat[-1].any()==0: mat = np.delete(mat, -1, 0) if mat[:,0].any()==0: mat = np.delete(mat, 0, 1) if mat[:,-1].any()==0: mat = np.delete(mat,-1, 1) return mat It works but I'm pretty sure there is a better method … Continue reading...