title: Create a similarity matrix of integers, using R
body: <p>I have a matrix with diagonals equal to zero and off-diagonals all equal to one (the inverse of an identity matrix):</p>
  
  <pre><code>mat1 &lt;- matrix(c(0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0), 5, 5)
  </code></pre>
  
  <p>I also have a vector that is always the same length as the dims of the matrix and always starts at zero:</p>
  
  <pre><code>vec1 &lt;- c(0,1,2,3,4)
  </code></pre>
  
  <p>using these two objects I want to create a matrix that looks like this:</p>
  
  <pre><code>mat2 &lt;- matrix(c(0,1,2,3,4,1,0,1,2,3,2,1,0,1,2,3,2,1,0,1,4,3,2,1,0), 5, 5)
  
       [,1] [,2] [,3] [,4] [,5]
  [1,]    0    1    2    3    4
  [2,]    1    0    1    2    3
  [3,]    2    1    0    1    2
  [4,]    3    2    1    0    1
  [5,]    4    3    2    1    0
  </code></pre>
  
  <p>I want an operation that will generalize so that if I have a matrix of dims 9 by 9, for example, and a vector of 0:8 I can achieve the equivalent result. Any ideas for how to approach this?</p>