Let's say I have a matrix that looks like this, and I convert it into a dist
class object (without diagonal), and then into a vector for later purposes.
m = matrix(c(0,1,2,3, 1,0,3,4, 2,3,0,5, 3,4,5,0), nrow=4)
#m:
[,1] [,2] [,3] [,4]
[1,] 0 1 2 3
[2,] 1 0 3 4
[3,] 2 3 0 5
[4,] 3 4 5 0
md = as.dist(m, diag=F)
# md:
1 2 3
2 1
3 2 3
4 3 4 5
mdv = as.vector(md)
# 1 2 3 3 4 5
I can access the original matrix as usual with []
, and I could easily access the one-dimensional index (of, for example row 3, col 2) using m[ 3+((2-1)*4) ]
. The dist object (and the vector) is one-dimensional, but composes only of the lower triangle of the original matrix (and also lacks one element from each original col/row, since the diagonal was removed).
How can I later access the equivalent element in the vector mdv
? So e.g. how could I access the equivalent of m[3,2]
(value 3) in the object mdv
? (Not by the value, since there can be duplicate values, but by the index) Related Q&A resolve similar problems with as.matrix
on the dist object, but that doesn't do it for me (since I need to deal with the vector).
dist
tomatrix
withas.matrix
– akrun