Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在numpy中使用one()获取数字数组?_Python_Matlab_Numpy - Fatal编程技术网

Python 如何在numpy中使用one()获取数字数组?

Python 如何在numpy中使用one()获取数字数组?,python,matlab,numpy,Python,Matlab,Numpy,嗨,我有一个在Matlab中生成以下序列的代码 [ones(1,6*2) 2 ones(1,6*2-1) 2 ones(1,6*2) 1] ans = Columns 1 through 18 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 Columns 19 through 36 1 1 1 1 1 1

嗨,我有一个在Matlab中生成以下序列的代码

[ones(1,6*2) 2 ones(1,6*2-1) 2 ones(1,6*2) 1]

ans =

  Columns 1 through 18
     1    1    1    1    1    1    1    1    1    1    1    1    2    1    1    1    1    1

  Columns 19 through 36
     1    1    1    1    1    1    2    1    1    1    1    1    1    1    1    1    1    1

  Columns 37 through 38
     1    1
我想在Python中生成类似的数字数组

我已尝试生成如下内容

ConvStride = [np.ones((12,),dtype=int),2,np.ones((11,),dtype=int),2,np.ones((12,),dtype=int),1]

Ans= [1 1 1 1 1... 1],2,[1 1 1 ... 1],2,[1 1 1 1....1],1

ConvStride = [np.ones((12,),dtype=int),2,np.ones((11,),dtype=int),2,np.ones((12,),dtype=int),1]
必需的

[1111…..12111…..121111…..11]


您能告诉我这方面的工作吗。

您可以使用类似的python语法创建一个列表,然后将其转换为numpy数组

import numpy as np

x = [1]*(6*2) + [2] + [1]*(6*2-1) + [2] + [1]*(6*2) + [1]
ans = np.array(x)
如果您想使用numpy完成所有操作,可以使用
hstack

np.hstack([np.ones(6*2, int), 2, np.ones(6*2-1, int), 2, np.ones(6*2, int), 1])

可以使用类似的python语法创建列表,然后将其转换为numpy数组

import numpy as np

x = [1]*(6*2) + [2] + [1]*(6*2-1) + [2] + [1]*(6*2) + [1]
ans = np.array(x)
如果您想使用numpy完成所有操作,可以使用
hstack

np.hstack([np.ones(6*2, int), 2, np.ones(6*2-1, int), 2, np.ones(6*2, int), 1])

使用
np.r

np.r_[np.ones(12,int),2,np.ones(11,int),2,np.ones(12,int)]
# array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1,
#        1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])

使用
np.r

np.r_[np.ones(12,int),2,np.ones(11,int),2,np.ones(12,int)]
# array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1,
#        1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])

哇!我从来没见过那个块裸体API@James很方便。还有
c_
s_
。哇。我从来没见过那个块裸体API@James很方便。还有
c_
s_
。MATLAB“自动”连接这些2d数组(在MATLAB中,即使标量也是2d)。在
numpy
中,必须显式连接数组,确保维度兼容。答案以各种方式调整标量的尺寸以实现这一点。MATLAB“自动”连接这些2d数组(甚至标量在MATLAB中也是2d)。在
numpy
中,必须显式连接数组,确保维度兼容。答案以各种方式调整标量的尺寸以实现这一点。