Home > Article > Web Front-end > JavaScript program to check if matrix is symmetrical
A symmetric matrix is a special case of a matrix in which both the matrix and the transpose of the matrix are the same. A matrix is a set of integers or numbers stored in a rectangular form, which is equivalent to a two-dimensional array. The transpose of a matrix is also a matrix obtained by replacing all rows with columns. We will get a matrix and have to print whether it is a symmetric matrix or not.
Mat = [[1, 2, 3], [2, 3, 8], [3, 8, 0]]
Yes, the given matrix is the symmetric matrix.
As we all know, a transposed matrix is a matrix in which columns are replaced with rows and rows are replaced with columns. Therefore, the first row here is the same as the first column, the second row is the same as the column, and the third row is the same as the column. .
Mat = [[1, 2, 3], [2, 3, 9], [3, 8, 0]]
No, the given matrix is not a symmetric matrix.
In the given matrix, the transposed matrix will be -
Trans: [[1, 2, 3], [2, 3, 8], [3, 9, 0]]
We can see that the second row and the third row or the second column and the third column are different.
Note - As we can see, the transpose of a given matrix can be formed by swapping rows and columns, which means if the dimension of the matrix is N*M, then the dimension of the matrix is the transpose The matrix will be M*N. This means that for a matrix to be symmetric, N must equal M, resulting in a square matrix.
In this method, we first obtain the transposed matrix by creating a new matrix and storing the elements in rows and columns. We will then simply iterate over both matrices and compare them. If they don't match at any index then we will return false, otherwise we will return true.
// function to find the transpose of the given matrix function getTranspose(mat){ // getting the number of rows present in the given matrix. var n = mat.length; // getting the number of columns present in the given matrix. var m = mat.length; // creating a new array to store the transpose matrix // new array will have m rows and n columns var transP = new Array(m) // traversing over the given matrix column-wise for(var i = 0;i < m; i++){ var cur = new Array(n); for(var j = 0; j<n; j++){ cur[j] = mat[j][i]; } transP[i] = cur; } // returing tranpose of the given matrix return transP; } // function to check if the given matrix is symmetric or not function check(mat){ var n = mat.length; var m = mat[0].length; // matrix must be a square matrix if(n != m){ return false; } // getting tranpose of the given matrix var transP = getTranspose(mat); // checking if both matrices are equal for(var i = 0; i<n ;i++){ for(var j = 0; j<n ;j++){ if(mat[i][j] != transP[i][j]){ return false; } } } return true; } // defining the matrix var mat = [[1, 2, 3], [2, 3, 8], [3, 8, 0]] console.log("The given matrix is: ") console.log(mat); if(check(mat)){ console.log("The given matrix is a symmetric matrix") } else{ console.log("The given matrix is not a symmetric matrix") }
The time complexity of the above code is O(N*N), where N is the size of the given matrix.
The space complexity of the above code is O(N*N) because we use extra space to store the transposed matrix elements.
The transposed matrix can be obtained by exchanging rows and columns, that is, each column is equal to the corresponding row. Therefore, the value at any index (i,j) will be equal to the value at (j,i) in the given matrix.
// function to check if the given matrix is symmetric or not function check(mat){ var n = mat.length; var m = mat[0].length; // matrix must be a square matrix if(n != m){ return false; } // checking if mat[i][j] is equal to mat[j][i] or not for(var i = 0; i<n ;i++){ for(var j = 0; j<i ;j++){ if(mat[i][j] != mat[j][i]){ return false; } } } return true; } // defining the matrix var mat = [[1, 2, 3], [2, 3, 8], [3, 9, 0]] console.log("The given matrix is: ") console.log(mat); if(check(mat)){ console.log("The given matrix is a symmetric matrix") } else{ console.log("The given matrix is not a symmetric matrix") }
The time complexity of the above code is O(N*N), where N is the size of the given matrix.
The space complexity of the above code is O(1) because we are not using any extra space.
In the above tutorial, we implemented a piece of JavaScript code to find whether a given matrix is a symmetric matrix. A symmetric matrix is a special case of a matrix in which both the matrix and the transpose of the matrix are the same, and the transpose of the matrix can be obtained by exchanging the rows and columns. A matrix must be square to be symmetric. We implemented two methods with time complexity of O(N*N), space complexity of O(N*N), and space complexity of O(1).
The above is the detailed content of JavaScript program to check if matrix is symmetrical. For more information, please follow other related articles on the PHP Chinese website!