开发者

Numpy array - duplicating rows and centering columns in a larger array

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 insi开发者_如何学运维de 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,


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]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜