R - How to get row & column subscripts of matched elements from a distance matrix
<p>I have an integer vector <code>vec1</code> and I am generating a distant matrix using <code>dist</code> function. I want to get the coordinates (row and column) of element of certain value in the distance matrix. Essentially I would like to get the pair of elements that are d-distant apart. For example:</p>

<pre><code>vec1 &lt;- c(2,3,6,12,17)
distMatrix &lt;- dist(vec1)

#   1  2  3  4
#2  1         
#3  4  3      
#4 10  9  6   
#5 15 14 11  5
</code></pre>

<p>Say, I am interested in pair of elements in the vector that are 5 unit apart. I wanted to get the coordinate1 which are the rows and coordinate2 which are the columns of the distance matrix. In this toy example, I would expect</p>

<pre><code>coord1  
# [1] 5
coord2
# [1] 4
</code></pre>

<p>I am wondering if there is an efficient way to get these values that doesn't involve converting the <code>dist</code> object to a matrix or looping through the matrix?   </p>