我想从最接近测试坐标的向量中提取坐标。
该任务将与先前发布的:(Find the approximate value in the vector)非常相似,但适用于nDimensional情况并具有多个输入。
换言之,鉴于:
test=t(data.frame(
c(0.9,1.1,1),
c(7.5,7.4,7.3),
c(11,11,11.2)
))
reference=t(data.frame(
c(1,0,0.5),
c(2,2,2),
c(3.3,3.3,3.3),
c(9,9,9),
c(10,11,12)
))
result <- approximate(test,reference)
1 0 0.5
9 9 9
10 11 12我用欧几里德距离和旧的学校循环编写了一个函数,但是当输入数据很大时,就会导致执行次数的增加。
有人能想出一种更有效的方法吗?提前谢谢你。
PS:这是我创建的函数,可以工作,但需要一段时间(以防有人发现它有用)。
approximate_function<- function(approximate,reference){
# Function that returns for each entrance of approximate the closest value of reference
# It uses a euclidean distance.
# each entrance must be a row in the dataframe
# the number of columns of the df indicates the dimension of the points
# Sub function to calculate euclidean distance
distance_function<- function(a,b){
squaresum<-0
for(id in 1:length(a)){
squaresum=squaresum+(a[id]-b[id])^2
}
result=sqrt(squaresum)
return(result)
}
result<-data.frame()
#Choose 1 item from vector to aproximate at a time
for(id_approximate in 1:nrow(approximate)){
distance=c()
#Compare the value to aproximate with the reference points and chose the one with less distance
for(id_reference in 1:nrow(reference)){
distance[id_reference]<-distance_function(approximate[id_approximate,],reference[id_reference,])
}
result<-rbind(
result,
reference[which.min(distance),]
)
}
return(result)
}发布于 2022-05-12 10:01:27
你有一些计算会很慢。
第一:
test=t(data.frame(
c(0.9,1.1,1),
c(7.5,7.4,7.3),
c(11,11,11.2)
))这个可能不重要,但最好是
test=rbind(
c(0.9,1.1,1),
c(7.5,7.4,7.3),
c(11,11,11.2)
)设置reference也是一样的。
第二和第三步:将result设置为数据格式,然后每次向其中添加行。对于行操作来说,Dataframes要比矩阵慢得多,并且R中的逐渐增长的结构是缓慢的。因此,从一开始就将其设置为一个矩阵,并将结果分配到特定的行中。
编辑后添加:
第四:不需要内部循环。您可以计算一个大矩阵中的所有平方差,然后使用rowSums或colSums求出平方距离。如果使用的是矩阵列而不是行,这是最简单的,因为向量将被正确地自动复制。
第五:不需要取平方根;如果平方距离被最小化,那么距离也是最小的。
结果如下:
approximate <- function(test, reference){
# transpose the reference
reference <- t(reference)
# set up the result, not transposed
result <- test*NA
#Choose 1 item from vector to aproximate at a time
for(id in seq_len(nrow(test))){
squareddist <- colSums((test[id,] - reference)^2)
result[id,] <- reference[, which.min(squareddist)]
}
return(result)
}发布于 2022-05-12 11:41:16
这样,计算就可以立即完成。
approximate_function<- function(approximate,reference){
# Function that returns for each entrance of approximate the closest value of reference
# It uses a euclidean distance.
# each entrance must be a row in the dataframe
# the number of columns of the df indicates the dimension of the points
results=data.frame()
#Choose 1 item from vector to aproximate at a time
for(id in 1:nrow(approximate)){
#calculates euclidean distances regardless the dimension
sumsquares=rep(0,nrow(reference))
for(dim in 1:ncol(approximate)){
sumsquares = sumsquares + (approximate[id,dim]-reference[,dim])^2
}
distances=sqrt(sumsquares)
results<- rbind(
results,
reference[which.min(distances),]
)
}
return(results)
}https://stackoverflow.com/questions/72213390
复制相似问题