[SOLVED] How to expand a numpy vector n times into an 2d array with its own values

Issue

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

Solution

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

Answered By – mozway

Answer Checked By – Robin (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *