Given a distance matrix, calculate the \(k\) nearest neighbors of each location, including the location itself. The matrix should contain only zeros on the diagonal, and all other elements should be positive.
Arguments
- x
A (square) distance matrix. Elements should be non-negative and the diagonal zeros, but this is not checked.
- k
The number of nearest neighbors, counting the location itself.