Extract diagonals from a distance matrix in R

后端 未结 2 444
礼貌的吻别
礼貌的吻别 2020-12-22 03:43

I would like to know how can I extract the values of the first diagonal from a distance matrix.

For example:

> mymatrix
     [,1] [,2]
[1,]    1           


        
2条回答
  •  伪装坚强ぢ
    2020-12-22 04:06

    One work around is to convert the dist object to matrix and then extract elements where row index is one larger than the column index:

    mat = as.matrix(dist(mymatrix))
    mat[row(mat) == col(mat) + 1]
    # [1] 2.828427 3.000000 2.828427
    

提交回复
热议问题