Transpose of a matrix is an operation in which we convert the rows of the matrix in column and column of the matrix in rows. The general equation for performing the transpose of a matrix is as follows.

Aij = Aji where i is not equal to j

**Example:**

Matrix M ---> [1, 8, 9 12, 6, 2 19, 42, 3] Transpose of M Output ---> [1, 12, 19 8, 6, 42, 9, 2, 3]

Transpose of a Matrix can be performed in two ways:

**Finding the transpose by using the t() function**`# R program for Transpose of a Matrix`

`# create a matrix with 2 rows`

`# using matrix() method`

`M <`

`-`

`matrix(`

`1`

`:`

`6`

`, nrow`

`=`

`2`

`)`

`# print the original matrix`

`print`

`(M)`

`# transpose of matrix`

`# using t() function.`

`t <`

`-`

`t(M)`

`# print the transpose matrix`

`print`

`(t)`

*chevron_right**filter_none***Output:**[, 1] [, 2] [, 3] [1, ] 1 3 5 [2, ] 2 4 6 [, 1] [, 2] [1, ] 1 2 [2, ] 3 4 [3, ] 5 6

**By iterating over each value using Loops:**`# R program for Matrix Transpose`

`# create a matrix of 2 rows`

`# using matrix() method.`

`M <`

`-`

`matrix(`

`1`

`:`

`6`

`, nrow`

`=`

`2`

`)`

`# print the matrix`

`print`

`(M)`

`# create another matrix`

`M2 <`

`-`

`M`

`# Loops for Matrix Transpose`

`for`

`(i`

`in`

`1`

`:nrow(M2))`

`{`

`# iterate over each row`

`for`

`(j`

`in`

`1`

`:ncol(M2))`

`{`

`# iterate over each column`

`# assign the correspondent elements`

`# from row to column and column to row.`

`M2[i, j] <`

`-`

`M[j, I]`

`}`

`}`

`# print the transposed matrix`

`print`

`(M2)`

*chevron_right**filter_none***Output:**[, 1] [, 2] [, 3] [1, ] 1 3 5 [2, ] 2 4 6 [, 1] [, 2] [1, ] 1 2 [2, ] 3 4 [3, ] 5 6

## Recommended Posts:

- Calculate the cross-product of the Transpose of a Matrix in R Programming - tcrossprod() Function
- Get Transpose of a Matrix or Data Frame in R Programming - t() Function
- Check if the Object is a Matrix in R Programming - is.matrix() Function
- Convert a Data Frame into a Numeric Matrix in R Programming - data.matrix() Function
- Convert an Object into a Matrix in R Programming - as.matrix() Function
- Transform the Scaled Matrix to its Original Form in R Programming - Using Matrix Computations
- Java Program to Determine if a given Matrix is a Sparse Matrix
- Getting the Modulus of the Determinant of a Matrix in R Programming - determinant() Function
- Return a Matrix with Lower Triangle as TRUE values in R Programming - lower.tri() Function
- Print matrix in zig-zag fashion from the last column
- How to read a Matrix from user in Java?
- Take Matrix input from user in Python
- Add and Remove vertex in Adjacency Matrix representation of Graph
- Compute Choleski factorization of a Matrix in R Programming - chol() Function
- Matrix vs Dataframe in R
- Inverse of Matrix in R
- Matrix Multiplication in R
- Create Matrix from Vectors in R
- Get or Set Dimensions of a Matrix in R Programming - dim() Function
- Algebraic Operations on a Matrix in R

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.