2011-04-21 31 views
0

我有大量的数组与维72,x其中x小于144.我想采取这些数组,并对他们做两件事:Numpy数组 - 复制行和居中列在一个更大的数组

  1. 复制原始中的每一行,使其中有144个。

  2. 中心阵列水平地较大144

最终结果内部是144x144阵列。我想使用numpy并尽可能避免循环(我已经可以在循环中实现这一点)。我已经四处搜寻,但还没有找到一个简洁的解决方案。

感谢,

回答

2

让我们小例子:

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.]]) 

因此,对于您的情况:

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

我真的很感激这段代码,遗憾的是在其最终形式,它是一点点对我来说太晦涩难懂了。出于这个原因,我决定将矩阵写成图像,然后使用ImageMagick: 'for * in * .png; do mogrify -geometry $(file $ f | cut -f2 -d,| cut -f1 -dx | tr -d“”)x144! -background black -extent 144x144 -gravity center $ f; done' – Brian 2011-04-21 22:05:40

相关问题