Loading content...
In linear algebra and matrix theory, one of the most essential operations is the row-column interchange of a matrix, commonly known as matrix transposition. This fundamental transformation reflects a matrix across its main diagonal, effectively swapping the row and column indices of every element.
Given a matrix A of dimensions m × n (where m is the number of rows and n is the number of columns), the row-column interchange produces a new matrix A^T of dimensions n × m. The element at position (i, j) in the original matrix moves to position (j, i) in the resulting matrix:
$$A^T_{ij} = A_{ji}$$
Geometric Interpretation: Visually, this operation can be understood as reflecting the matrix across its main diagonal (the line running from the top-left to the bottom-right). For a rectangular matrix, this also changes the shape—a wide matrix becomes tall, and vice versa.
Key Properties:
Your Task: Write a Python function that performs the row-column interchange on a given 2D matrix. The function should:
matrix = [[1, 2, 3], [4, 5, 6]][[1, 4], [2, 5], [3, 6]]The input is a 2×3 matrix (2 rows, 3 columns). After the row-column interchange:
• The first row [1, 2, 3] becomes the first column • The second row [4, 5, 6] becomes the second column
Element mapping: • matrix[0][0] = 1 → result[0][0] = 1 • matrix[0][1] = 2 → result[1][0] = 2 • matrix[0][2] = 3 → result[2][0] = 3 • matrix[1][0] = 4 → result[0][1] = 4 • matrix[1][1] = 5 → result[1][1] = 5 • matrix[1][2] = 6 → result[2][1] = 6
The resulting 3×2 matrix is [[1, 4], [2, 5], [3, 6]].
matrix = [[1, 2], [3, 4]][[1, 3], [2, 4]]This is a 2×2 square matrix. The row-column interchange swaps elements across the main diagonal:
• Elements on the main diagonal (1 and 4) stay in place • matrix[0][1] = 2 swaps with matrix[1][0] = 3
The original matrix: The result: | 1 2 | | 1 3 | | 3 4 | → | 2 4 |
Notice how the off-diagonal elements exchanged positions while diagonal elements remained fixed.
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]][[1, 4, 7], [2, 5, 8], [3, 6, 9]]This is a 3×3 square matrix. The row-column interchange reflects elements across the main diagonal:
• Diagonal elements (1, 5, 9) remain unchanged at positions (0,0), (1,1), (2,2) • Upper triangle elements move to lower triangle: 2→(1,0), 3→(2,0), 6→(2,1) • Lower triangle elements move to upper triangle: 4→(0,1), 7→(0,2), 8→(1,2)
Original: Result: | 1 2 3 | | 1 4 7 | | 4 5 6 | → | 2 5 8 | | 7 8 9 | | 3 6 9 |
This demonstrates the reflection across the main diagonal (from top-left to bottom-right).
Constraints