i am calculating the distance between two categorical vectors (i.e. character vectors) as follows:
我正在计算两个分类向量(即字符向量)之间的距离,如下所示:
Distanz <- function(Ref,Inp){
y <- numeric(length=1)
for(i in 1:length(Ref)){
if(Ref[i] != Inp[i]){y <- y+1}
}
return(y)
}
Obviously the vectors have the same length. The result is the number of dimensions in which the two vectors differ.
显然,矢量具有相同的长度。结果是两个向量不同的维数。
But i am having performance problems. Does anyone have an idea how to fasten this calculation?
但我遇到了性能问题。有没有人知道如何加紧这个计算?
Thanks, Lukas
谢谢,卢卡斯
1 个解决方案
#1
1
It's not clear what size of vectors you are dealing with, or what too slow means, but this is just the hamming distance, right? Does this work
目前尚不清楚你正在处理的矢量大小,或者说太慢的意思,但这只是汉明距离,对吗?这有用吗
sum(Ref != Inp)
#1
1
It's not clear what size of vectors you are dealing with, or what too slow means, but this is just the hamming distance, right? Does this work
目前尚不清楚你正在处理的矢量大小,或者说太慢的意思,但这只是汉明距离,对吗?这有用吗
sum(Ref != Inp)