[Numpy-discussion] is there a better way to do this array repeat?

Chris Colbert sccolbert@gmail....
Sun Aug 16 19:01:56 CDT 2009


I don't think np.repeat will do what I want because the order needs to
be preserved.

I have a 1x3 array that I want to repeat n times and form an nx3 array
where each row is a copy of the original array.

So far I have this:

>>> import numpy as np
>>> a = np.arange(3)
>>> b = np.asarray([a]*10)
>>> b
array([[0, 1, 2],
       [0, 1, 2],
       [0, 1, 2],
       [0, 1, 2],
       [0, 1, 2],
       [0, 1, 2],
       [0, 1, 2],
       [0, 1, 2],
       [0, 1, 2],
       [0, 1, 2]])
>>> b.shape
(10, 3)


the issue is that my n may be very large and I'd rather not make that
list if I don't have to.


Cheers,

Chris


More information about the NumPy-Discussion mailing list