Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python | Numpy matrix.var()
Next article icon

With the help ofNumpy matrix.transpose() method, we can find the transpose of the matrix by using thematrix.transpose()method inPython.

Numpy matrix.transpose() Syntax

Syntax :matrix.transpose()

Parameter: No parameters; transposes the matrix it is called on.

Return : Return transposed matrix

What is Numpy matrix.transpose()?

`numpy.matrix.transpose()` is a function in theNumPy library that computes the transpose of a matrix. It swaps the rows and columns of the matrix, effectively reflecting it along its main diagonal. The function is called on a NumPy matrix object, and it does not take any parameters. The result is a new matrix representing the transposed version of the original matrix.

NumPy Matrix transpose() - Transpose of an Array in Python

There are numerous examples of `numpy.matrix.transpose()`. Here, we illustrate commonly used instances of `numpy.matrix.transpose()` for clarity.

  • Matrix Transformation
  • Matrix Multiplication

NumPy Matrix Transformation

Example 1: In this example, the code uses the NumPy library to create a 2x3 matrix. It then calculates the transpose of the original matrix using the `transpose()` function. Finally, it prints both the original and transposed matrices to the console.

Python3
importnumpyasnp# Original matrixoriginal_matrix=np.matrix([[1,2,3],[4,5,6]])# Transposed matrixtransposed_matrix=original_matrix.transpose()print("Original Matrix:")print(original_matrix)print("\nTransposed Matrix:")print(transposed_matrix)

Output:

Original Matrix:[[1 2 3] [4 5 6]]Transposed Matrix:[[1 4] [2 5] [3 6]]

Example 2: In this example This Python code uses the NumPy library to create a 3x3 matrix named 'gfg'. It then applies the transpose() method to the matrix and assigns the result to 'geek'. Finally, it prints the transposed matrix 'geek'.

Python3
# import the important module in pythonimportnumpyasnp# make matrix with numpygfg=np.matrix('[4, 1, 9; 12, 3, 1; 4, 5, 6]')# applying matrix.transpose() methodgeek=gfg.transpose()print(geek)

Output:

[[ 4 12  4] [ 1  3  5] [ 9  1  6]]

Transpose of an Array Like Object

In this example code uses NumPy to create two 2x2 matrices, `matrix_a` and `matrix_b`. It then transposes `matrix_b` using the `transpose()` function and performs matrix multiplication between `matrix_a` and the transposed `matrix_b`. The result is printed to the console.

Python3
importnumpyasnp# Matrices for multiplicationmatrix_a=np.matrix([[1,2],[3,4]])matrix_b=np.matrix([[5,6],[7,8]])# Transpose one matrix before multiplicationresult=matrix_a*matrix_b.transpose()print("Result of Matrix Multiplication:")print(result)

Output:

Result of Matrix Multiplication:[[17 23] [39 53]]

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp