Вы можете использовать outer
.
M <- +(outer(v, v, `==`))
M[50:60, 50:60]
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
# [1,] 1 0 0 0 0 0 0 0 0 0 0
# [2,] 0 1 1 0 1 1 1 1 1 1 1
# [3,] 0 1 1 0 1 1 1 1 1 1 1
# [4,] 0 0 0 1 0 0 0 0 0 0 0
# [5,] 0 1 1 0 1 1 1 1 1 1 1
# [6,] 0 1 1 0 1 1 1 1 1 1 1
# [7,] 0 1 1 0 1 1 1 1 1 1 1
# [8,] 0 1 1 0 1 1 1 1 1 1 1
# [9,] 0 1 1 0 1 1 1 1 1 1 1
# [10,] 0 1 1 0 1 1 1 1 1 1 1
# [11,] 0 1 1 0 1 1 1 1 1 1 1
v[50:60]
# [1] 1 2 2 3 2 2 2 2 2 2 2
Данные:
v <- kmeans_res$cluster