
对称矩阵是矩阵的特殊情况,其中矩阵和矩阵的转置都相同。矩阵是一组以矩形形式存储的整数或数字,相当于二维数组,矩阵的转置也是将所有行替换为列得到的矩阵。我们将得到一个矩阵,并且必须打印它是否是对称矩阵。
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]]
我们可以看到第二行和第三行或者第二列和第三列不一样。
立即学习“Java免费学习笔记(深入)”;
注意 - 正如我们所见,给定矩阵的转置可以通过交换行和列来形成,这意味着如果矩阵的维度为 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)。
以上就是检查矩阵是否对称的 JavaScript 程序的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号