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.
enter
Mat = [[1, 2, 3], [2, 3, 8], [3, 8, 0]]
Output
Yes, the given matrix is the symmetric matrix.
illustrate
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. .
enter
Mat = [[1, 2, 3], [2, 3, 9], [3, 8, 0]]
Output
No, the given matrix is not a symmetric matrix.
illustrate
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.
Naive method
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.
Example
// 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") }
Time and space complexity
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.
Efficient method
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.
Example
// 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") }
Time and space complexity
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 conclusion
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

10 fun jQuery game plugins to make your website more attractive and enhance user stickiness! While Flash is still the best software for developing casual web games, jQuery can also create surprising effects, and while not comparable to pure action Flash games, in some cases you can also have unexpected fun in your browser. jQuery tic toe game The "Hello world" of game programming now has a jQuery version. Source code jQuery Crazy Word Composition Game This is a fill-in-the-blank game, and it can produce some weird results due to not knowing the context of the word. Source code jQuery mine sweeping game

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

This tutorial demonstrates how to create a captivating parallax background effect using jQuery. We'll build a header banner with layered images that create a stunning visual depth. The updated plugin works with jQuery 1.6.4 and later. Download the

This tutorial demonstrates creating dynamic page boxes loaded via AJAX, enabling instant refresh without full page reloads. It leverages jQuery and JavaScript. Think of it as a custom Facebook-style content box loader. Key Concepts: AJAX and jQuery

This JavaScript library leverages the window.name property to manage session data without relying on cookies. It offers a robust solution for storing and retrieving session variables across browsers. The library provides three core methods: Session
