Mirror of Matrix Across Diagonal in JavaScript



To write a javascript program for mirror of matrix across diagonal, we will be discussing two approaches with their code example and explanation of code.

In this article we are having a 2D matrix. Our task is to write a JavaScript program for mirror of matrix across diagonal.

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 is a list of approaches to change the position of the scrollbar using CSS which we will be discussing in this article with stepwise explanation and complete example codes.

Using a New Matrix

For mirror of matrix across diagonal we have used a new matrix in this approach to store the mirrored elements of original matrix.

  • We have declares a 2D matrix mat and defined a function mirror() that accepts mat as argument.
  • We have got the matrix size using length property and declared an empty matrix mirrorMatrix where we will store the mirrored matrix elements.
  • Using nested for loop, we traverse the matrix. We store the ith row and jth column of matrix in jth row and ith column of mirrorMatrix creating a mirror image across diagonal.
  • As the loop ends mirrorMatrix is returned and the result is displayed in web console using console.log() method.

Example

Here is a complete example code implementing above mentioned steps for mirror of matrix across diagonal using a new matrix.

let mat = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];
console.log("Original matrix is:");
console.log(mat);

function mirror(mat) {
    let n = mat.length;
    let mirrorMatrix = [];

    for (let i = 0; i 


Using Swapping

In this approach we have used swapping of elements. We have ith row with jth column and jth column with ith row.

  • We have declares a 2D matrix mat and defined a function mirror that accepts mat as argument.
  • We have got the matrix size using length property and declared an empty matrix mirrorMatrix where we will store the mirrored matrix elements.
  • Then we have used nested for loop to traverse the matrix and swap the value of mat[i][j] with mat[j][i].
  • Using this we can create mirror of matrix without creating a new matrix.
  • As the loop ends mat is returned and the result is displayed in web console using console.log() method.

Example

Here is a complete example code implementing above mentioned steps for mirror of matrix across diagonal by swapping.

let mat = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];
console.log("Original matrix is:");
console.log(mat);

function mirror(mat) {
    let n = mat.length;
    for (let i = 0; i 


Complexity Comparison

Here is a comparison of time and space complexity of all the above approaches.

Approach Time Complexity Space Complexity
Using a New Matrix O(n^2) O(n)
Using Swapping O(n^2) O(1)

Conclusion

In this article we discussed two different approaches to write a javascript program for mirror of matrix across diagonal. These approaches are: by using a new matrix and by swapping. Both the approaches has the time complexity of O(n^2) but the second approach is space efficient as it has space complexity of O(1).

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.
Updated on: 2025-01-24T15:14:27+05:30

348 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements