Python: Concatenate (or clone) a numpy array N times
You are close, you want to use np.tile
, but like this:
a = np.array([0,1,2])np.tile(a,(3,1))
Result:
array([[0, 1, 2], [0, 1, 2], [0, 1, 2]])
If you call np.tile(a,3)
you will get concatenate
behavior like you were seeing
array([0, 1, 2, 0, 1, 2, 0, 1, 2])
http://docs.scipy.org/doc/numpy/reference/generated/numpy.tile.html
You could use vstack:
numpy.vstack([X]*N)
e.g.
>>> import numpy as np >>> X = np.array([1,2,3,4]) >>> N = 7 >>> np.vstack([X]*N) array([[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]])
Have you tried this:
n = 5X = numpy.array([1,2,3,4])Y = numpy.array([X for _ in xrange(n)])print YY[0][1] = 10print Y
prints:
[[1 2 3 4] [1 2 3 4] [1 2 3 4] [1 2 3 4] [1 2 3 4]][[ 1 10 3 4] [ 1 2 3 4] [ 1 2 3 4] [ 1 2 3 4] [ 1 2 3 4]]