Reputation: 65
I have two vectors x
and cen
. Say the length of x
is 10
and the length of cen
is 3
. I want to find the Euclidean distance between x
and individual points of cen
.
The Euclidean distance function is defined as : euc.dist <- function(x1, x2) sqrt(sum((x1 - x2) ^ 2))
I saw that sapply works with the negation "-"
operator like so:
> x
[1] 23 4 65 8 9 23 90 76 55 7
> cen
[1] 23 4 65
> sapply(x,"-",cen)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 0 -19 42 -15 -14 0 67 53 32 -16
[2,] 19 0 61 4 5 19 86 72 51 3
[3,] -42 -61 0 -57 -56 -42 25 11 -10 -58
Here each each value within the cen
vector operates over whole x
vector.
But when I try to use the euc.dist
function instead of "-"
then I only get a single vector as an output instead of a 3x10 matrix.
> sapply(x,"euc.dist",cen)
[1] 46.09772 63.89053 74.06079 59.07622 57.93962 46.09772 111.84811 90.07774 61.03278 60.24118
Any reasons as to why this is happening? Do I need to define the euc.dist function in a different manner? Any help would be appreciated. Thanks in advance.
Upvotes: 1
Views: 153
Reputation: 13314
Your euc.dist
function is not vectorize, as could be easily tested by:
euc.dist(x[1],cen)
[1] 46.09772
whereas what you probably want is:
> Vectorize(euc.dist)(x[1],cen)
[1] 0 19 42
(Use ?Vectorize
for the details).
So, in your case a simple solution would be:
> sapply(x,Vectorize(euc.dist),cen)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 0 19 42 15 14 0 67 53 32 16
[2,] 19 0 61 4 5 19 86 72 51 3
[3,] 42 61 0 57 56 42 25 11 10 58
Upvotes: 2
Reputation: 2952
how about this? since you want all factor combinations
datas <- expand.grid(x = x,cen = cen)
datas$euclid = apply(datas,1,function(rowval) euc.dist(rowval[1], rowval[2]))
Upvotes: 1