tags:

views:

618

answers:

2

Hi, just a quick question, if I have a matrix has n rows and m columns, how can I cut off the 4 sides of the matrix and return a new matrix? (the new matrix would have n-2 rows m-2 columns).

Thanks in advance

+4  A: 
a[1:-1, 1:-1]
Mr Fooz
That is fantastically compact! I had only previously seen this done using a tuple of slice(1,-1) objects.
Chris Cameron
numpy has some really nice ways of handling indexing and slicing. I miss the more advanced slicing features when I use Matlab (esp. the broadcasting features).
Mr Fooz
+2  A: 

A more general answer is:

a[[slice(1, -1) for _ in a.shape]]
J.F. Sebastian