In: Computer Science
In python find matrix M
I have the matrix X
X=[5.8 3.0 3.7 1.9]
and need to create a matrix M that is 4x150 that has the the values of matrix X inside it for every row
M=
5.8 | 3.0 | 3.7 | 1.9 |
5.8 | 3.0 | 3.7 | 1.9 |
5.8 | 3.0 | 3.7 | 1.9 |
5.8 | 3.0 | 3.7 | 1.9 |
5.8 | 3.0 | 3.7 | ect |
5.8 | ect | ect | |
5.8 | |||
ect | |||
5.8 | |||
The given matrix is:
X=[5.8 3.0 3.7 1.9]
We need to create one more matrix that has 150 rows and 4 columns and then will initialize the value from the matrix X to the newly created matrix.
The required program source code is given below:
import numpy as np
#create an array
X = np.array([5.8, 3.0, 3.7, 1.9])
#create a matrix
M = np.zeros((150, 4))
#initialize the matrix
for i in range(150):
for j in range(4):
M[i][j] = X[j]
#display the matrix
for i in range(150):
for j in range(4):
print(M[i][j],end=" ")
print()
The screenshot of the above source code is given below:
OUTPUT:
5.8 3.0 3.7 1.9
5.8 3.0 3.7 1.9
5.8 3.0 3.7 1.9
5.8 3.0 3.7 1.9
5.8 3.0 3.7 1.9
continue up to 150 rows...