title: Replacing non-diagonal elements in a matrix in R (hopefully a better asked this time)
body: <p>Okay, I asked this question earlier but I got bashed (deservedly) for not specifying anything and showing no sign of previous attempt. So let me try again..</p>
  
  <p>I'm using R, and I have a 463✕463 matrix. What I would like to do is to replace all elements other than the diagonal ones (X<sub>11</sub>, X<sub>22</sub>, X<sub>33</sub>,...,X<sub>jj</sub>) with zero.</p>
  
  <p>E.g. I want:</p>
  
  <pre><code>[1 4 5
   2 3 5
   3 9 8]
  </code></pre>
  
  <p>to be:</p>
  
  <pre><code>[1 0 0
   0 3 0
   0 0 8]
  </code></pre>
  
  <p>When I use the <code>diag()</code> function, it simply gives me a column vector of the diagonal values. I imagine I can use the <code>replace()</code> function somehow combined with a "if not diagonal" logic...but I am lost. </p>
  
  <p>And yes, as some here have guessed, I am probably much younger than many people here and am completely new at this...so please put me in the right direction. Really appreciate all your help!</p>