0

I have a large number of arrays with dimension 72, x where x is less than 144. I'd like to take these arrays and do two things to them:

  1. Duplicate each row in the original so that there are 144 of them.

  2. Center the arrays horizontally inside a larger 144

The end result is a 144x144 array. I'd like to use numpy and to the greatest extent possible avoid loops ( I can already implement this in loops ). I've searched around but haven't found a neat solution yet.

Thanks,

1 Answer 1

2

Let's take a smaller example:

import numpy as np
a = np.array([[1, 2],
              [3, 4]])

b = np.zeros((4,4))

b[:,1:-1] = np.repeat(a, 2, axis=0)

# returns:

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

So for your case:

a = np.arange(5184).reshape(72,72)
b = np.zeros((144,144))
b[36:-36,:] = np.repeat(a, int(144 / a.shape[0]) + 1, axis=1)[:,:144]
Sign up to request clarification or add additional context in comments.

1 Comment

I really appreciate this code, unfortunately in its final form it was a little bit too obscure for me to grok. For that reason I settled on writing the matrices as images and then using ImageMagick: for f in *.png; do mogrify -geometry $(file $f | cut -f2 -d, | cut -f1 -dx | tr -d " ")x144! -background black -extent 144x144 -gravity center $f; done

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.