Smithey
Smithey

Reputation: 337

Creating matrix with the same vector in each row

I have p = [1,2,3,4]. I would like a 100x4 numpy matrix with p in each row. What's the best way to create that?

I tried pvect = np.array(p for i in range(10)) but that doesn't seem to be right.

Upvotes: 0

Views: 1182

Answers (3)

Hanno Reuvers
Hanno Reuvers

Reputation: 636

Using matrix algebra: the multiplication of a column vector of ones times your row vector p effectively places the vector p in each row.

p = np.array([[1,2,3,4]])
OutputArray = np.ones((100, 1)) @ p

Upvotes: 2

mozway
mozway

Reputation: 260390

Use numpy.tile:

pvect = np.tile(p, (100, 1))

output:

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

Upvotes: 3

99_m4n
99_m4n

Reputation: 1265

you can try:

pvect = np.array(p*100).reshape((100,4))

Upvotes: 1

Related Questions