Programmer
Programmer

Reputation: 8727

Numpy: What is the correct way to upsample an array?

octave:1> a=[1 2 3]
a =

   1   2   3

octave:2> k=[a;zeros(9,length(a))]
k =

   1   2   3
   0   0   0
   0   0   0
   0   0   0
   0   0   0
   0   0   0
   0   0   0
   0   0   0
   0   0   0
   0   0   0

Is the below method the correct way to achieve it in Python:

>>> a=[1, 2, 3]
>>> np.append(a,np.zeros((9,len(a))))
array([ 1.,  2.,  3.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.,  0.])

Upvotes: 0

Views: 1524

Answers (1)

João Abrantes
João Abrantes

Reputation: 4883

The octave solution results in a 10x3 matrix while your solution results in a 1-dimensional array with 30 elements. I am assuming you want a matrix with the dimensions 10x3 right?

>>>a=np.array((1, 2, 3))
>>>k=np.vstack((a,np.zeros((9,len(a)))))
array([[ 1.,  2.,  3.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])

Upvotes: 3

Related Questions