And my solution as well:
Let data be matrix m:
x y
[1,] 2 3
[2,] 5 6
[3,] 3 2
[4,] 5 1
[5,] 4 1
[6,] 6 8
Then centers are given by:
cnt = c(mean(m[,1]),mean(m[,2]))
So the code returning vector of distance between every row of m and cnt will be:
apply(m,1,function(x,cnt) {(sqrt((x[1] - cnt[1])^2+(x[2]-cnt[2])^2))},cnt)
And the result is:
[1] 2.223611 2.635231 1.900292 2.635231 2.505549 4.859127
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…