JavaScript 程序检查矩阵是否对称
对称矩阵是矩阵的一种特殊情况,其中矩阵与其转置矩阵相同。矩阵是一组以矩形形式存储的整数或数字,等价于二维数组,矩阵的转置也是一个矩阵,我们可以通过用列替换所有行来获得它。我们将得到一个矩阵,并必须打印它是否是对称矩阵。
输入
Mat = [[1, 2, 3], [2, 3, 8], [3, 8, 0]]
输出
Yes, the given matrix is the symmetric matrix.
解释
众所周知,转置矩阵是列替换为行,行替换为列的矩阵,因此这里第一行与第一列相同,第二行、列和第三行、列也相同。
输入
Mat = [[1, 2, 3], [2, 3, 9], [3, 8, 0]]
输出
No, the given matrix is not a symmetric matrix.
解释
在给定的矩阵中,转置矩阵将为 -
Trans: [[1, 2, 3], [2, 3, 8], [3, 9, 0]]
我们可以看到第二行或第三行与第二列或第三列不同。
注意 - 正如我们所看到的,给定矩阵的转置可以通过相互交换行和列来形成,这意味着如果矩阵的维度是 N*M,则转置矩阵的维度将是 M*N。这意味着对于矩阵成为对称矩阵,N 必须等于 M,并且它会导致方阵。
朴素方法
在这种方法中,我们将首先通过创建一个新矩阵并将行的元素按列存储来获取转置矩阵。然后,我们将简单地遍历这两个矩阵并进行比较。如果它们在任何索引处不匹配,则返回 false,否则返回 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")
}
时间和空间复杂度
上述代码的时间复杂度为 O(N*N),其中 N 是给定矩阵的大小。
上述代码的空间复杂度为 O(N*N),因为我们使用额外的空间来存储转置矩阵元素。
有效方法
转置矩阵可以通过交换行和列来获得,这意味着每一列都等于相应的行。因此,在任何索引 (i,j) 处存在的值将等于在给定矩阵中的 (j,i) 处存在的值。
示例
// 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")
}
时间和空间复杂度
上述代码的时间复杂度为 O(N*N),其中 N 是给定矩阵的大小。
上述代码的空间复杂度为 O(1),因为我们没有使用任何额外空间。
结论
在上面的教程中,我们实现了一个 JavaScript 代码来查找给定矩阵是否是对称矩阵。对称矩阵是矩阵的一种特殊情况,其中矩阵与其转置矩阵相同,并且可以通过交换行和列来获取矩阵的转置。矩阵必须是方阵才能成为对称矩阵。我们已经实现了两种方法,时间复杂度为 O(N*N),空间复杂度分别为 O(N*N) 和 O(1)。
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP