title: Create a program that sums the diagonal of a matrix
body: <p>I'm trying to create a program that sums the diagonal of a matrix that I've created. I used the code below but I don't understand whats wrong with the code I made.</p>
  <pre><code>a &lt;- c(1, 2, 3) 
  b &lt;- c(3, 5, 6)
  
  x &lt;- matrix(a, b, nrow=3, ncol=3)
  x
  
  for (i in 1:nrow(x)) {
    for (j in 1:ncol(x)) {
      if (i=j) {
        diags[i, j] &lt;- sum(x[i, j])
      }
    }
  }
  </code></pre>