python用新的大小重塑[matlab-like]

python用新的大小重塑[matlab-like],python,matlab,reshape,Python,Matlab,Reshape,我正在将matlab代码翻译成python代码 我想像matlab一样实现重塑 matlab代码为: reshape(array,size1,[]) 我想用一个输入大小重塑2D形状。如何在python中实现它?您可以使用numpy来执行大多数可以使用matlab import numpy as np # create a random 1D array of size 100 array = np.random.randint(1,100,100) # reshape the array to

我正在将matlab代码翻译成python代码

我想像matlab一样实现重塑

matlab代码为:

reshape(array,size1,[])

我想用一个输入大小重塑2D形状。如何在python中实现它?

您可以使用
numpy
来执行大多数可以使用
matlab

import numpy as np
# create a random 1D array of size 100
array = np.random.randint(1,100,100)
# reshape the array to a 2D form
array = np.reshape(array, (2,50))
# reshape the array back to the 1D form
array = np.reshape(array, (100,))

要扩展Ali的答案,可以使用-1代替任何维度来计算它的大小。例如:

array = np.random.randint(1,100,100)
array_reshaped = array.reshape(2, -1)

array\u重塑后的
将是一个2 x 50的数组。

我想通过M(自动计算)将数组重塑为N(输入)。你的建议只是为了在numpy中正常使用重塑。所以你需要编辑你的问题,添加更多细节来解释你到底想要什么。我只是想找到一种简单的方法将matlab代码翻译成python。我想知道python是否有这个方法函数。