Home > other >  Creating matrix with the same vector in each row
Creating matrix with the same vector in each row

Time:08-07

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.

Thanks

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

you can try:

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