Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
JavaScript Program for Mirror of Matrix Across Diagonal
To write a javascript program for mirror of matrix across diagonal, we will be discussing two approaches with their code examples and explanations.
In this article we have a 2D matrix. Our task is to write a JavaScript program that creates a mirror of the matrix across its diagonal. This operation is also known as matrix transpose, where rows become columns and columns become rows.
Example
Input:
matrix = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
Output:
[[1, 4, 7],
[2, 5, 8],
[3, 6, 9]]
Approaches for Mirror of Matrix Across Diagonal
Here are two approaches to create a mirror of matrix across diagonal with stepwise explanation and complete example codes:
Using a New Matrix
This approach creates a new matrix to store the mirrored elements of the original matrix. The element at position [i][j] in the original matrix is placed at position [j][i] in the new matrix.
- We declare a 2D matrix mat and define a function mirror() that accepts mat as argument.
- We get the matrix size using length property and declare an empty matrix mirrorMatrix.
- Using nested for loops, we traverse the matrix and store mat[i][j] at mirrorMatrix[j][i].
- The function returns the new mirrored matrix.
Example
let mat = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
console.log("Original matrix:");
console.log(mat);
function mirror(mat) {
let n = mat.length;
let mirrorMatrix = [];
for (let i = 0; i < n; i++) {
mirrorMatrix[i] = [];
for (let j = 0; j < n; j++) {
mirrorMatrix[i][j] = mat[j][i];
}
}
return mirrorMatrix;
}
console.log("Mirror of the matrix across diagonal:");
console.log(mirror(mat));
Original matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Mirror of the matrix across diagonal: [ [ 1, 4, 7 ], [ 2, 5, 8 ], [ 3, 6, 9 ] ]
Using In-Place Swapping
This approach modifies the original matrix by swapping elements across the diagonal. We swap mat[i][j] with mat[j][i] for all elements above the diagonal.
- We declare a 2D matrix mat and define a function mirror() that accepts mat as argument.
- We get the matrix size using the length property.
- Using nested for loops, we traverse only the upper triangle of the matrix (j starts from i+1).
- We swap mat[i][j] with mat[j][i] using a temporary variable.
- This creates the mirror without needing additional space.
Example
let mat = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
console.log("Original matrix:");
console.log(mat);
function mirror(mat) {
let n = mat.length;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
let temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
}
}
return mat;
}
console.log("Mirror of the matrix across diagonal:");
console.log(mirror(mat));
Original matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Mirror of the matrix across diagonal: [ [ 1, 4, 7 ], [ 2, 5, 8 ], [ 3, 6, 9 ] ]
Complexity Comparison
Here is a comparison of time and space complexity of both approaches:
| Approach | Time Complexity | Space Complexity | Modifies Original |
|---|---|---|---|
| Using a New Matrix | O(n²) | O(n²) | No |
| Using In-Place Swapping | O(n²) | O(1) | Yes |
Conclusion
Both approaches effectively create a mirror of the matrix across its diagonal. The in-place swapping method is more space-efficient with O(1) space complexity, while the new matrix approach preserves the original matrix but requires additional O(n²) space.
Practice and learn from a wide range of JavaScript examples, including event handling, form validation, and advanced techniques. Interactive code snippets for hands-on learning.
