如何使用 NumPy (Python) 截断矩阵
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/365395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 19:57:53 来源:igfitidea点击:
How to truncate matrix using NumPy (Python)
提问by NONEenglisher
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).
只是一个简单的问题,如果我有一个矩阵有 n 行和 m 列,我怎样才能切断矩阵的 4 边并返回一个新矩阵?(新矩阵将有 n-2 行 m-2 列)。
Thanks in advance
提前致谢
回答by Mr Fooz
a[1:-1, 1:-1]
回答by jfs
A more general answer is:
更一般的答案是:
a[[slice(1, -1) for _ in a.shape]]