Matrix transposition is a fundamental operation in linear algebra where the rows and columns of a matrix are interchanged. If an original matrix A has dimensions M x N (M rows, N columns), its transpose, denoted A^T, will have dimensions N x M (N rows, N columns). Each element A[i][j] in the original matrix becomes A^T[j][i] in the transposed matrix.
Consider a 2x3 matrix, represented as sourceMatrix:
sourceMatrix = {{1, 2, 3},
{4, 5, 6}}
| 0 | 1 | 2 | |
|---|---|---|---|
| 0 | 1 | 2 | 3 |
| 1 | 4 | 5 | 6 |
Its transpose, transposedMatrix, will be a 3x2 matrix where rows and columns are swapped:
transposedMatrix = {{1, 4},
{2, 5},
{3, 6}}
| 0 | 1 | |
|---|---|---|
| 0 | 1 | 4 |
| 1 | 2 | 5 |
| 2 | 3 | 6 |
C Implementation for Matrix Transposition
The following C program demonstrates how to transpose a 2D array by iterating through the original matrix and assigning elements to a new matrix with swapped indices.
#include <stdio.h>
// Define the dimensions of the original matrix
#define ORIGINAL_ROWS 2
#define ORIGINAL_COLS 3
int main() {
// Initialize the source 2D array (matrix)
int sourceMatrix[ORIGINAL_ROWS][ORIGINAL_COLS] = {
{1, 2, 3},
{4, 5, 6}
};
// Declare the transposed matrix with swapped dimensions
int transposedMatrix[ORIGINAL_COLS][ORIGINAL_ROWS];
int r_idx, c_idx; // Loop indices for rows and columns
// --- Display the Original Matrix ---
printf("Original Matrix (%d x %d):\n", ORIGINAL_ROWS, ORIGINAL_COLS);
for (r_idx = 0; r_idx < ORIGINAL_ROWS; ++r_idx) {
for (c_idx = 0; c_idx < ORIGINAL_COLS; ++c_idx) {
printf("%4d", sourceMatrix[r_idx][c_idx]);
}
printf("\n");
}
printf("\n");
// --- Perform the Matrix Transposition ---
// Iterate through the original matrix's rows
for (r_idx = 0; r_idx < ORIGINAL_ROWS; ++r_idx) {
// Iterate through the original matrix's columns
for (c_idx = 0; c_idx < ORIGINAL_COLS; ++c_idx) {
// Assign element from source[row][col] to transposed[col][row]
transposedMatrix[c_idx][r_idx] = sourceMatrix[r_idx][c_idx];
}
}
// --- Display the Transposed Matrix ---
printf("Transposed Matrix (%d x %d):\n", ORIGINAL_COLS, ORIGINAL_ROWS);
// Note: The loops for printing the transposed matrix use its new dimensions
for (r_idx = 0; r_idx < ORIGINAL_COLS; ++r_idx) { // Now iterating up to original columns
for (c_idx = 0; c_idx < ORIGINAL_ROWS; ++c_idx) { // Now iterating up to original rows
printf("%4d", transposedMatrix[r_idx][c_idx]);
}
printf("\n");
}
return 0;
}
Code Explanation
The program begins by defining constants ORIGINAL_ROWS and ORIGINAL_COLS for clarity and maintainability. The sourceMatrix is initialized with sample values, and transposedMatrix is declared with its dimensions swapped (ORIGINAL_COLS rows and ORIGINAL_ROWS columns).
The first set of nested for loops is solely for printing the contents of the sourceMatrix to the console, demonstrating its initial state.
The core of the transposition logic resides in the subsequent nested for loops:
for (r_idx = 0; r_idx < ORIGINAL_ROWS; ++r_idx) {
for (c_idx = 0; c_idx < ORIGINAL_COLS; ++c_idx) {
transposedMatrix[c_idx][r_idx] = sourceMatrix[r_idx][c_idx];
}
}
Here, the outer loop iterates through each row of the sourceMatrix (controlled by r_idx), and the inner loop iterates through each column (controlled by c_idx). For every element sourceMatrix[r_idx][c_idx], its assigned to the corresponding position in transposedMatrix[c_idx][r_idx]. This operation directly implements the definition of a matrix transpose: swapping the row and column indices. For example, the element at sourceMatrix[0][1] (which is 2) is moved to transposedMatrix[1][0].
Finally, another set of nested for loops is used to display the transposedMatrix. It's crucial that these loops reflect the new dimensions of the transposed matrix, meaning the outer loop runs from 0 to ORIGINAL_COLS - 1 and the inner loop runs from 0 to ORIGINAL_ROWS - 1, to correct iterate through the transposed structure.