在Numpy中连接空数组

10 浏览
0 Comments

在Numpy中连接空数组

在Matlab中我这样做:

>> E = [];
>> A = [1 2 3 4 5; 10 20 30 40 50];
>> E = [E ; A]
E =
     1     2     3     4     5
    10    20    30    40    50

现在我想在Numpy中做同样的事情,但是遇到了问题,请看下面的内容:

>>> E = array([],dtype=int)
>>> E
array([], dtype=int64)
>>> A = array([[1,2,3,4,5],[10,20,30,40,50]])
>>> E = vstack((E,A))
Traceback (most recent call last):
  File "", line 1, in 
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/shape_base.py", line 226, in vstack
    return _nx.concatenate(map(atleast_2d,tup),0)
ValueError: array dimensions must agree except for d_0

当我使用以下代码时,我遇到了类似的情况:

>>> E = concatenate((E,A),axis=0)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: arrays must have same number of dimensions

或者:

>>> E = append([E],[A],axis=0)
Traceback (most recent call last):
  File "", line 1, in 
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/lib/function_base.py", line 3577, in append
    return concatenate((arr, values), axis=axis)
ValueError: arrays must have same number of dimensions

0