Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add n zeros to the end of an array

I want to add n zeros to an array.

When your array is x, and you want to add 3 zeros at the and of an array without creating 2 arrays:

x = np.array([1.0, 2.0, 1.0, 2.0, 7.0, 9.0, 1.0, 1.0, 3.0, 4.0, 10.0])

I thought this command would be helpful:

x = [x, np.zeros(N)]

But I recieved 2 arrays instead of 1 big array:

[array([  1.,   2.,   1.,   2.,   7.,   9.,   1.,   1.,   3.,   4.,  10.]),
 array([ 0.,  0.,  0.,  0.])]

So I want to create this type of array:

[array([  1.,   2.,   1.,   2.,   7.,   9.,   1.,   1.,   3.,   4.,  10., 0.,  0.,  0.,  0.])]

Can anyone help me with this rather simple question?

like image 894
itseva Avatar asked Jul 31 '17 17:07

itseva


1 Answers

You can use numpy.pad, which pads default 0 to both ends of the array while in constant mode, specify the pad_width = (0, N) will pad N zeros to the right and nothing to the left:

N = 4
np.pad(x, (0, N), 'constant')
#array([  1.,   2.,   1.,   2.,   7.,   9.,   1.,   1.,   3.,   4.,  10.,
#         0.,   0.,   0.,   0.])
like image 198
Psidom Avatar answered Oct 04 '22 20:10

Psidom