如何检查矩阵是否对称?

时间:2019-04-15 17:49:42

标签: java arrays matrix

我必须将矩阵垂直分割成两半,看看它是否对称。例如,如果mat [0] [0] == mat [0] [1]和mat [1] [0] == mat [1] [1],则矩阵是对称的。我设法检查了2x2矩阵的对称性,但是我希望该函数在任何类型的矩阵(例如4x4或3x3)上都可用。

i和j是我用来遍历mat元素的变量,而n是矩阵的维数。这是2x2矩阵的函数。我该如何概括呢?

 private static void getSymmetry(int mat[][], int i, int j, int n) {
        int index = 0;
        int sum=0;
        if (n == 2) {
            if (mat[i][j] == mat[i][j + 1]) {
                index = index + 1;
            }
            if (mat[i + 1][j] == mat[i + 1][j + 1]) {
                index = index + 1;
            }
            sum = sum + index;
        }
        System.out.println("Degree of symmetry is " + sum);
    }

4 个答案:

答案 0 :(得分:1)

您可以使用以下方法检查所有数字的对称性:

let selected = [];
let filter = ['item-1', 'item-2', 'item-4', 'item-5', 'item-7', 'item-8', 'item-3'];

let data = [
  ['item-0', 'item-112', 'item-4', 'item-5', 'item-7', 'item-8', 'item-3'],
  ['item-1', 'item-12', 'item-4', 'item-5', 'item-7', 'item-8', 'item-3'],
  ['item-1', 'item-2', 'item-4', 'item-5', 'item-7', 'item-8', 'item-31'],
  ['item-1', 'item-2', 'item-4', 'item-5', 'item-7', 'item-8', 'item-3'],
  ['item-1', 'item-2', 'item-4', 'item-5', 'item-7', 'item-8', 'item-3', 'item-3'],
  ['item-1', 'item-2', 'item-4', 'item-5', 'item-7', 'item-8', 'item-3', 'item-3', 'item-3', 'item-3']

];

function checkfilter(filter, data) {
  return  data.map(a=> filter.every(f=>a.includes(f)));
}

console.log(checkfilter(filter, data) );

...

private static boolean isSymmetric(int mat[][]) {
    for(int a = 0; a< mat.length; a++){
        for(int b = 0; b < mat[a].length / 2; b++){
            if(mat[a][b] != mat[a][mat[a].length-b-1]) {
                return false;
            }
        }
    }       
    return true;
}

答案 1 :(得分:0)

尽管提供的答案很好,但这是使用称为Two-Pointers的已知技术的另一种尝试。我相信虽然它更加清晰和优化。

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        int[][] mat = {
            {1, 2, 2, 1},
            {4, 3, 3, 4},
            {2, 3, 3, 2}
        };

        //assume the matrix is square
        int rows = mat.length, columns = mat[0].length;

        boolean symmetric = true;
        for(int r = 0; r < rows && symmetric; r++){
            //now declare two pointers one from left and one from right
            int left = 0, right = columns - 1;

            while (left < right){
                if(mat[r][left] != mat[r][right]){
                    symmetric = false;
                    break;
                }
                right--;
                left++;
            }
        }

        System.out.println(symmetric? "The matrix is symmetric." : "The matrix isn't symmetric.");
    }
}

这是Ideone代码。

答案 2 :(得分:0)

public class MatrixSymmetry {

  public static void main(String[] args) {

    int[][] matrix = {
        {1, 1, 1},
        {1, 2, 1},
        {1, 1, 1}
    };

    System.out.println(isSymmetric(matrix));
  }

  public static boolean isSymmetric(int[][] matrix) {

    for (int i = 0; i < matrix.length; i++) {

      // you got the row
      int[] row = matrix[i];
      int end = row.length - 1;

      for (int start = 0; start < end; start++) {
        if (row[start] != row[end]) {
          return false;
        }
        end--;
      }

    }
    return true;
  }

}

答案 3 :(得分:0)

这里是检查对称矩阵的一种不太复杂且有效的方法。每当矩阵中存在不相等的维数或匹配项时,它将中断循环,然后返回false;否则,如果没有不匹配项,则返回true。

public boolean isMatrixSymmetric(int [][] matrix){
        for (int x = 0; x < matrix.length; x++) {
            if(matrix.length != matrix[x].length){
                return false;
            }
            for (int y = 0; y < matrix[x].length; y++) {
                if(matrix[y][x] != matrix[x][y]){
                    return false;
                } 
            }
        }
        return true;
}