在MATLAB中,只需输入
L = 2^8
x = (-L/2:L/2-1)';
创建一个大小为L X 1的数组。
如何在Python中创建这个?
我试着:
L = 2**8
x = np.arange(-L/2.0,L/ 2.0)
不能用
给你:
x.reshape((-1,1))
MATLAB代码生成一个(1,n)大小的矩阵,将其转置为(n,1)
>> 2:5
ans =
2 3 4 5
>> (2:5)'
ans =
2
3
4
5
MATLAB矩阵总是2d(或更高)。numpy
可以是1d,也可以是0d。
https://numpy.org/doc/stable/user/numpy-for-matlab-users.html
Innumpy
:
arange
生成1d数组:
In [165]: np.arange(2,5)
Out[165]: array([2, 3, 4])
In [166]: _.shape
Out[166]: (3,)
在数组中添加末尾维度的方法有很多:
In [167]: np.arange(2,5)[:,None]
Out[167]:
array([[2],
[3],
[4]])
In [168]: np.arange(2,5).reshape(3,1)
Out[168]:
array([[2],
[3],
[4]])
numpy
有一个转置,但它对一维数组的行为与人们对二维数组的期望不同。它实际上比MATLAB的'
更强大,更通用。