Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

255
Views
How to check if the input matrix is symmetric?

The code below takes in a matrix (MAT) and transposes the matrix, calls it array. The definition of the symmetrical matrix is that it should be a square matrix and the elements in the given matrix compared to the transposed one should be the same.

The given matrix below and transposed matrix should output false if checked for symmetry.

I did create an if statement at first to check whether MAT[j][i] and array[j][i] are the same but keep getting the wrong answer. It's not properly checking all the elements together. Could someone help with that?

Thanks!

const symmetricMatrix = function (MAT) {
  let array = [];
  for (let i = 0; i < MAT.length; i++) {
    array.push([]);
    for (let j = 0; j < MAT.length; j++) {
      array[i].push(MAT[j][i]);

    }
  }

  
  return array;
};

console.log(
  symmetricMatrix(
    (MAT = [
      [1, 3, 1],
      [-1, 1, 4],
      [2, 1, 0],
    ])
  )
);

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

First you can create a copy of matrix and then transpose it and then check if it has same element at that index.

const symmetricMatrix = function (mat) {
  const copy = Array.from(mat, (_) => []);

  for (let i = 0; i < mat.length; i++)
    for (let j = 0; j < mat.length; j++) 
      copy[i][j] = mat[j][i];

  for (let i = 0; i < mat.length; i++)
    for (let j = 0; j < mat.length; j++)
      if (copy[i][j] != mat[i][j]) return false;

  return true;
};

const matrix = [
  [1, 3, 1],
  [-1, 1, 4],
  [2, 1, 0],
];
const matrix2 = [
  [1, -1, 2],
  [-1, 1, 1],
  [2, 1, 0],
];
console.log(symmetricMatrix(matrix));
console.log(symmetricMatrix(matrix2));

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!