title: How to assign values to a MATLAB matrix on the diagonal?
body: <p>Suppose I have an NxN matrix A, an index vector V consisting of a subset of the numbers 1:N, and a value K, and I want to do this:</p>
  
  <pre><code> for i = V
       A(i,i) = K
   end
  </code></pre>
  
  <p>Is there a way to do this in one statement w/ vectorization?</p>
  
  <p>e.g. A(<strong>something</strong>) = K</p>
  
  <p>The statement <code>A(V,V) = K</code> will not work, it assigns off-diagonal elements, and this is not what I want. e.g.:</p>
  
  <pre><code>&gt;&gt; A = zeros(5);
  &gt;&gt; V = [1 3 4];
  &gt;&gt; A(V,V) = 1
  
  A =
  
   1     0     1     1     0
   0     0     0     0     0
   1     0     1     1     0
   1     0     1     1     0
   0     0     0     0     0
  </code></pre>