python - replicating borders in 2d numpy arrays -


i trying replicate border of 2d numpy array:

>>> numpy import * >>> test = array(range(9)).reshape(3,3) array([[0, 1, 2],        [3, 4, 5],        [6, 7, 8]]) 

is there easy way replicate border in direction?

for example:

>>>> replicate(test, idx=0, axis=0, n=3)  array([[0, 1, 2],        [0, 1, 2],        [0, 1, 2],        [0, 1, 2],        [3, 4, 5],        [6, 7, 8]]) 

edit:

the following function did job:

def replicate(a, xy, se, n):     rptidx = numpy.ones(a.shape[0 if xy == 'x' else 1], dtype=int)     rptidx[0 if se == 'start' else -1] = n + 1     return numpy.repeat(a, rptidx, axis=0 if xy == 'x' else 1) 

with xy in ['x', 'y'] , se in ['start', 'end']

you can use np.repeat:

in [5]: np.repeat(test, [4, 1, 1], axis=0) out[5]:  array([[0, 1, 2],        [0, 1, 2],        [0, 1, 2],        [0, 1, 2],        [3, 4, 5],        [6, 7, 8]]) 

but larger/variable arrays more difficult define repeats argument ([4, 1, 1], in case how many times want repeat each row).


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -