The apply function with margin = 1 seems the most obvious:
> x [,1] [,2] [,3] [1,] 0 2 1 [2,] 1 1 1 > apply(x , 1, function(z) crossprod(z, 1:length(z) ) ) [1] 7 6 > 2*2+1*3 [1] 7 > 1*1+2*1+3*1 [1] 6
So, if you need distances, then the square root of cross-producing differences at the selected point seems to work:
> apply(x , 1, function(z) sqrt(sum(crossprod(z -c(0,2,2), zc(0,2,2) ) ) ) ) [1] 1.000000 1.732051
source share