Open In App

How to Transpose a Matrix Without Using t() Function in R

Last Updated : 02 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will Transpose a Matrix without using t() function in R Programming Language.

Transpose of a matrix is an operation in which we convert the rows of the matrix into columns and columns 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 ---> [1, 2, 3
             4, 5, 6
             7, 8, 9]
              
Transpose of Matrix
 --->   [1,4,7
         2,5,8
         3,6,9]

Example:

Create 3*3 matrix and transpose.

R




# create matrix with 3 rows and 3 columns
Matrix = matrix(1:9, nrow = 3)
  
# print the matrix
print(Matrix)
  
# create another matrix
M2 = Matrix
  
# 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] <- Matrix[j, i]
    }
}
  
# print the transposed matrix
print(M2)


Output:

 


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads