How to truncate matrix using NumPy (Python)
Question
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
Solution
a[1:-1, 1:-1]
OTHER TIPS
A more general answer is:
a[[slice(1, -1) for _ in a.shape]]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow