首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义对引用值的舍入(nDimensional)

自定义对引用值的舍入(nDimensional)
EN

Stack Overflow用户
提问于 2022-05-12 09:46:06
回答 2查看 27关注 0票数 1

我想从最接近测试坐标的向量中提取坐标。

该任务将与先前发布的:(Find the approximate value in the vector)非常相似,但适用于nDimensional情况并具有多个输入。

换言之,鉴于:

代码语言:javascript
复制
 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:这是我创建的函数,可以工作,但需要一段时间(以防有人发现它有用)。

代码语言:javascript
复制
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)
    

}
EN

回答 2

Stack Overflow用户

发布于 2022-05-12 10:01:27

你有一些计算会很慢。

第一:

代码语言:javascript
复制
 test=t(data.frame(
    c(0.9,1.1,1),         
    c(7.5,7.4,7.3),
    c(11,11,11.2)
  ))

这个可能不重要,但最好是

代码语言:javascript
复制
test=rbind(
      c(0.9,1.1,1),
      c(7.5,7.4,7.3),
      c(11,11,11.2)
    )

设置reference也是一样的。

第二和第三步:将result设置为数据格式,然后每次向其中添加行。对于行操作来说,Dataframes要比矩阵慢得多,并且R中的逐渐增长的结构是缓慢的。因此,从一开始就将其设置为一个矩阵,并将结果分配到特定的行中。

编辑后添加:

第四:不需要内部循环。您可以计算一个大矩阵中的所有平方差,然后使用rowSumscolSums求出平方距离。如果使用的是矩阵列而不是行,这是最简单的,因为向量将被正确地自动复制。

第五:不需要取平方根;如果平方距离被最小化,那么距离也是最小的。

结果如下:

代码语言:javascript
复制
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)
  
}
票数 0
EN

Stack Overflow用户

发布于 2022-05-12 11:41:16

这样,计算就可以立即完成。

代码语言:javascript
复制
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)

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72213390

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档