Transpose of a Square Matrix
A matrix with an equal number of rows and columns is called a square matrix. In other words, the number of rows and columns in a square matrix is equal. An “n x n” matrix is used to represent it, with “n” standing for both the number of rows and columns.
R
# Create a square matrix square_matrix <- matrix (1:9, nrow = 3, ncol = 3) print ( "Square Matrix:" ) print (square_matrix) # Initialize a matrix for the transpose transpose_square_matrix <- matrix (0, nrow = ncol (square_matrix), ncol = nrow (square_matrix)) # Calculate the transpose using nested for loops for (i in 1: ncol (square_matrix)) { for (j in 1: nrow (square_matrix)) { transpose_square_matrix[i, j] <- square_matrix[j, i] } } print ( "Transpose of Square Matrix:" ) print (transpose_square_matrix) |
Output
[1] "Square Matrix:"
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
[1] "Transpose of Square Matrix:"
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
Reverse matrix in R
A transpose of a matrix is a new matrix that is obtained by swapping the rows and columns of the original matrix. In R, you can calculate the transpose of a matrix using nested for loops to iterate through the elements and rearrange them accordingly. Transposing a matrix is a fundamental operation in linear algebra and is useful in various mathematical and data analysis applications.
Contact Us