Matrix Transpose in detail
The transpose of a matrix is an operation that swaps its rows and columns. This means that the element at position (i,j)(i, j)(i,j) in the original matrix moves to position (j,i)(j, i)(j,i) in the transposed matrix. The general equation is:
Aij = Aji Where i ≠ j
Transpose of M:
Methods to Find the Transpose of a Matrix in R
1. Using the t() Function
The simplest way to find the transpose of a matrix in R is by using the built-in t() function.
# Create a matrix with 2 rows
Matrix_1 <- matrix(c(10, 20, 30, 40, 50, 60), nrow = 2)
# Print the original matrix
print(Matrix_1)
# Transpose the matrix using t() function
Transpose_1 <- t(Matrix_1)
# Print the transposed matrix
print(Transpose_1)
Output:
[,1] [,2] [,3]
[1,] 10 30 50
[2,] 20 40 60
[,1] [,2]
[1,] 10 20
[2,] 30 40
[3,] 50 60
2. Using Loops to Compute Transpose Manually
We can also compute the transpose by iterating over each element and swapping rows with columns.
# Create a 3x3 matrix
Matrix_2 <- matrix(c(2, 4, 6, 8, 10, 12, 14, 16, 18), nrow = 3)
# Print the original matrix
print(Matrix_2)
# Create another matrix to store the transpose
Transpose_2 <- Matrix_2
# Loop for Matrix Transpose
for (i in 1:nrow(Transpose_2)) {
for (j in 1:ncol(Transpose_2)) {
Transpose_2[i, j] <- Matrix_2[j, i]
}
}
# Print the transposed matrix
print(Transpose_2)
Output:
[,1] [,2] [,3]
[1,] 2 8 14
[2,] 4 10 16
[3,] 6 12 18
[,1] [,2] [,3]
[1,] 2 4 6
[2,] 8 10 12
[3,] 14 16 18
Leave a Reply