title: How to change the values of the diagonal of a matrix in numpy?
body: <p>I would like to set the diagonal of some <code>numpy</code> matrix to an arbitrary 1D array.</p>
  
  <p>For example, if:</p>
  
  <pre><code>mat = np.array([[1,2],[3,4]])
  diag = np.array([5,6])
  </code></pre>
  
  <p>then:</p>
  
  <pre><code>&gt;&gt;&gt; set_diagonal(mat, diag)
  &gt;&gt;&gt; mat
  ... array([[5,2],
             [3,6]])
  </code></pre>
  
  <p>I <em>could</em> create a diagonal matrix from the <code>diag</code> variable, <code>diag_fill</code> the <code>mat</code> matrix to <code>0</code> and add the results.  However, this seems to use a bunch of unnecessary resources if I'm trying to alter the diagonal in place.</p>
  
  <p>How do I set the diagonal of a matrix in <code>numpy</code>?</p>
  
  <p>Note: this is NOT a duplicate of <a href="https://stackoverflow.com/questions/9958577/changing-the-values-of-the-diagonal-of-a-matrix-in-numpy">this question</a>, as they want to set the diagonal to a <em>constant</em>.</p>