Reputation: 65
I have to split a matrix in half by vertical and to see if it is symmetric. For example, if mat[0][0]==mat[0][1] and mat[1][0]==mat[1][1], the matrix is symemtric. I have managed to check the symmetry on a 2x2 matrix, but I want the function to be available at any kind of matrix, 4x4 or 3x3 for example.
i and j are variables i use to get through the elements of mat and n is the dimension of the matrix. This is the function for a 2x2 matrix. How can I generalise it?
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);
}
Upvotes: 1
Views: 4936
Reputation: 358
Here is a less complex and efficient way of checking a symmetric matrix. It breaks the loop whenever there is an unequal dimension or match in the matrix then returns false otherwise true if there were no mismatch.
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;
}
Upvotes: 0
Reputation: 16698
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;
}
}
Upvotes: 0
Reputation: 798
You can use below approach it checks symmetric for all numbers:
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;
}
...
boolean symmetric = isSymmetric(mat);
if(symmetric) {
System.out.println("symmetric");
} else {
System.out.println("Not symmetric");
}
Upvotes: 1
Reputation: 9806
Although the provided answers are good, here is another attempt using a known technique called Two-Pointers
. I believe though that it is a bit more clear and optimized.
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.");
}
}
Here is the Ideone code.
Upvotes: 0