Home > Software engineering >  How to expand a numpy vector n times into an 2d array with its own values
How to expand a numpy vector n times into an 2d array with its own values

Time:03-15

With numpy, say you have a vector like:

array([1, 2, 3])

How do you extend it n times with its own values? E.g.:

array([[1, 2, 3],
       [1, 2, 3],
       [1, 2, 3]])

CodePudding user response:

You can use numpy.tile:

a = np.array([1, 2, 3])
N = 3

b = np.tile(a, (N,1))

or numpy.vstack:

N = 3
b = np.vstack([a]*N)

output:

array([[1, 2, 3],
       [1, 2, 3],
       [1, 2, 3]])
  • Related