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?
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.])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With