用Numpy解释Matlab索引/切片

2024-04-27 14:41:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在将一些Matlab代码转换成Python,我发现了一行我无法理解的代码:

Y = reshape(X(j+(1:a*b),:),[b,a,p])

我知道reshape函数有一个numpy analog,我也读过Matrix Indexing in MATLAB文档,但我似乎无法理解这行代码,无法将其转换为numpy索引/切片。在

我尝试了online converter OMPC,但它使用了functions that are not defined outside of it(就像mslice):

^{pr2}$

我也尝试过SMOP converter,但结果也很难理解:

Y = reshape(X(j + (arange(1, dot(a, b))), arange()), concat([b, a, p]))

你能解释一下在简单的Matlab中转换成numpy索引/切片规则吗?在


Tags: 函数代码in文档numpy切片matrixconverter
2条回答
Y = X[j+np.arange(a*b),:].reshape((b,a,p))

在不知道您到底想要什么的情况下,这是将matlab行翻译成python。在

注意,matlab索引从1开始,而numpy的索引从0开始。因此,根据其他行,内线可以是np.arange(a*b)或{}。在

另外,您不需要使用第二个索引X,因此X[1,:]==X[1]是{}

在八度音阶中:

>> 1:3*3
ans =
   1   2   3   4   5   6   7   8   9

在numpy ipython中:

^{pr2}$

相关问题 更多 >