1

I have the following Array a = np.array([1, 2, 3, 4, 9, 8, 7, 6]) now I have to add 2/12/22 zeros to add it to a Matrix with 10/20/30 columns.

It should look like that: a = np.array([1, 2, 3, 4, 9, 8, 7, 6]) -> a = np.array([1, 2, 3, 4, 0, 0, ..., 0, 0, 9, 8, 7, 6])

Im using np.pad to fill with zeros on the borders of an array, but is there a way to do it the other way around?

2 Answers 2

1

You may combine the hstack function with the slicing of the original matrix, so you can stack the beginning of the matrix, the zeroes, and the remaining of the matrix:

a = np.array([1, 2, 3, 4, 9, 8, 7, 6])
result = np.hstack((a[0:4], np.zeros(12), a[4:]))
Sign up to request clarification or add additional context in comments.

Comments

0

This is mission for numpy.insert function, example (2 zeros):

a = np.array([1, 2, 3, 4, 9, 8, 7, 6])
a = np.insert(a,4,np.zeros(2))
print(a) #prints [1 2 3 4 0 0 9 8 7 6]

Explanation: you can read that insert as: get array a, add beyond 4th element: 2 zeros.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.