import numpy n = 3 A = numpy.random.randint(low=1, high=10, size=(n, n)) A_transpose = A.transpose() B = (A + A_transpose)/2 print("A: \n", A) print("Symmetric Matrix B: \n", B)
Here, we are using numpy.random.randint() to generate a matrix that consists of random integers. The generated integers will be within the range [low, high). And the size argument specifies the size of the matrix. So, size=(n,n) indicates that the created matrix will be an nxn square matrix.
After that, we are using the transpose() function to calculate the transpose of the created matrix. And then we are using the following Python statement to create a symmetric matrix.
A_transpose = A.transpose() B = (A + A_transpose)/2
The output of the mentioned program will be:
A: [[6 7 8] [7 6 8] [8 4 2]] Symmetric Matrix B: [[6. 7. 8.] [7. 6. 6.] [8. 6. 2.]]






0 Comments