Compute Principal Diagonal Sum of a Javascript Matrix (Node)

Asked

Viewed 880 times

1

I’m trying to calculate the main diagonal of an array in Js, someone could help ?

function calcularDiagonal(matriz) {
  let soma = 0;
  for(let i = 0; i < matriz.length; i++) {
    for(let j = 0; j < matriz.length; j++) {
      if( matriz[i] == matriz[j]) {
        soma += matriz[i];
      }
    }
  }
  return soma;
}

const matriz = [[1,2,3,4,4,4,5,5,5]];

console.log(calcularDiagonal(matriz));

I did so but is returning a wrong result, which is :

01,2,3,4,4,4,5,5,5

But it should give the result 10, if anyone can see what’s wrong, thank you.

  • Diagonal is a property of square matrices. No diagonal non-square matrices are calculated. The matrix in your example is a line matrix therefore without diagonal.

  • 1

    @Augustovasques removed my answer. Virgilio’s is fine and I already gave it to him +1 :)

1 answer

4


It seems that lack concept, then: Square matrix is a special type of matrix that has the same number of rows and the same number of columns, and every square matrix has two diagonals: Main Diagonal and Secondary Diagonal, example:

inserir a descrição da imagem aqui

and its main diagonal is: a11 = 12, a22 = 6, a33 = 0 e a44 = 15.

Computationally an example of code:

const matriz = [
  [12,-8,15,6],
  [1,6,20,-9],
  [0,-4,0,-8],
  [-1,6,0,15]
];

function calculo(matriz) {
  const count = matriz.length;
  let result = 0;
  for(let i = 0; i < count; i++) {
    result += matriz[i][i];
  }
  return result;
}

console.log(calculo(matriz));
<table border="1" width="100%">
  <tr>
    <td>12</td>
    <td>-8</td>
    <td>15</td>
    <td>6</td>
  </tr>
  <tr>
    <td>1</td>
    <td>6</td>
    <td>20</td>
    <td>-9</td>
  </tr>
  <tr>
    <td>0</td>
    <td>-4</td>
    <td>0</td>
    <td>-8</td>
  </tr>
  <tr>
    <td>-1</td>
    <td>6</td>
    <td>0</td>
    <td>15</td>
  </tr>
</table>

Its main diagonal are the numbers 12, 6, 0, 15 respectively the positions 1,1 - 2,2 - 3,3 and 4,4 is sum is 33 as demonstrated in the code this sum only happens in quadratic matrix, that is, the same number of rows is equal to the same number of columns and vice versa (3x3, 4x4, etc);

  • Attention that in a rectangular matrix matriz[i][i] will have a parameter undefined

  • 2

    @Sergio a rectangular matrix is not a square matrix as stated in the question, that is, as already explained it has to have the same number of columns equal to the number of rows and rectangular does not fit

  • 1

    @Sergio: I’m trying to calculate the main diagonal of a matrix in Js only square matrix has this.

  • 1

    Guys, I was confused and it was just adding with matrix[i][j], with an array in js being const matrix = [[1,2,3],[4,4,4],[5,5,5,5]];

  • 1

    I wanted to add a square matrix and was setting it wrong in JS, thank you :)

  • 1

    I corrected it, I had accepted the wrong :)

  • 1

    Virgilio, okay I hadn’t noticed that detail! +1 @ Augustovasques my answer was not wrong, it is only more generalist :)

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.