Concatenating empty array in Numpy

后端 未结 6 478
情歌与酒
情歌与酒 2021-01-30 16:04

in Matlab I do this:

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

E =

     1     2     3     4     5
    10    20    30    4         


        
6条回答
  •  醉梦人生
    2021-01-30 16:39

    if you know the number of columns before hand:

    >>> xs = np.array([[1,2,3,4,5],[10,20,30,40,50]])
    >>> ys = np.array([], dtype=np.int64).reshape(0,5)
    >>> ys
    array([], shape=(0, 5), dtype=int64)
    >>> np.vstack([ys, xs])
    array([[  1.,   2.,   3.,   4.,   5.],
           [ 10.,  20.,  30.,  40.,  50.]])
    

    if not:

    >>> ys = np.array([])
    >>> ys = np.vstack([ys, xs]) if ys.size else xs
    array([[ 1,  2,  3,  4,  5],
           [10, 20, 30, 40, 50]])
    

提交回复
热议问题