Transpose of a Rectangular Matrix
One kind of matrix that does not have an equal number of rows and columns is a rectangle matrix. To put it another way, a rectangular matrix has a different number of rows than columns. An “m x n” matrix is used to represent it, where “m” stands for the number of rows and “n” stands for the number of columns. The fact that the matrix is not square is emphasised by the use of the adjective “rectangular”.
R
# Create a rectangular matrix rectangular_matrix <- matrix (1:8, nrow = 2, ncol = 4) print ( "Rectangular Matrix:" ) print (rectangular_matrix) # Initialize a matrix for the transpose transpose_rectangular_matrix <- matrix (0, nrow = ncol (rectangular_matrix), ncol = nrow (rectangular_matrix)) # Calculate the transpose using nested for loops for (i in 1: ncol (rectangular_matrix)) { for (j in 1: nrow (rectangular_matrix)) { transpose_rectangular_matrix[i, j] <- rectangular_matrix[j, i] } } print ( "Transpose of Rectangular Matrix:" ) print (transpose_rectangular_matrix) |
Output
[1] "Rectangular Matrix:"
[,1] [,2] [,3] [,4]
[1,] 1 3 5 7
[2,] 2 4 6 8
[1] "Transpose of Rectangular Matrix:"
[,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 5 6
[4,] 7 8
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.