1

I have an array of matrices all of different sizes/dimensions and I need to pad them all to the same size (805, 16866). Bellow is the code I'm currently using to do this but I believe it could be vectorised, however I am not sure how to do this.

x1 = np.zeros((805, 16866))

for i in range(x[0].shape[0]):
    for j in range(x[0].shape[1]):
        x1[i, j] = x[0][i, j]
1
  • You can use np.pad. Commented Mar 18, 2019 at 17:59

2 Answers 2

2

In the above example, x1[:x[0].shape[0], :x[0].shape[1]] = x should work

Sign up to request clarification or add additional context in comments.

Comments

0

There's the numpy.pad function (docs) which does what you need.

The code you want is

x1 = np.zeros((805,16866))

for i in range(x.shape[0])
  x1[i,:]= np.pad(x[i,:],x1.shape[0],'constant',constant_values=0)

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.