簡體   English   中英

找到R中所有其他點的最遠點

[英]Find most distant point all other points in R

我無法找到這個簡單問題的解決方案。 我一直在搜索論壇,並且我已經接近答案,這不是我需要的。

我試圖從一組x,y點找到哪個點距離任何其他點最遠,即不是點之間的最大距離,而是距離其余點最遠的點。

我試過了

x <-c(x1,x2,x3....)
y <-c(y1,y2,y3...)
dist(cbind(x,y))

這給了我每個點到每個點之間距離的矩陣。 我可以在MS Excel中查詢數據並找到答案。 找到每列中的最小值,然后找到它們之間的最大值。

在此輸入圖像描述

如果我要繪制數據,我希望輸出紅色或藍色線的距離(取決於哪個更長)。

在此輸入圖像描述

從此示例數據集開始:

set.seed(100)
x <- rnorm(150)
y <- rnorm(150)
coord <- cbind(x,y)
dobj <- dist(coord)

現在dobj是一個距離對象,但你不能直接檢查它。 您必須先將其轉換為矩陣,並確保您不考慮點與其自身之間的零距離:

dmat <- as.matrix(dobj)
diag(dmat) <- NA

后一行用NA替換距離矩陣中的對角線值。

現在你可以使用amonk的解決方案:

dmax <- max(apply(dmat,2,min,na.rm=TRUE))

這將為您提供到最近點的最大距離。 如果您想知道這些是哪些點,您可以采取額外步驟:

which(dmat == dmax, arr.ind = TRUE)
#     row col
# 130 130  59
# 59   59 130

因此,第130和59點是滿足您條件的兩點。 繪制圖表可以為您提供:

id <- which(dmat == dmax, arr.ind = TRUE) 
plot(coord)
lines(coord[id[1,],], col = 'red')

請注意如何獲得此信息兩次,因為兩點之間的歐幾里德距離是對稱的(A - > B與B - > A一樣長)。

在此輸入圖像描述

因此,對於df作為初始數據框,您可以執行以下操作:

df<-NULL#initialize object 
for(i in 1:10)#create 10 vectors with 10 pseudorandom numbers each
  df<-cbind(df,runif(10))#fill the dataframe

cordf<-cor(df);diag(cordf)<-NA #create correlation matrix and set diagonal values to NA

因此:

             [,1]        [,2]        [,3]        [,4]        [,5]        [,6]        [,7]        [,8]        [,9]       [,10]
[1,]          NA -0.03540916 -0.29183703  0.49358124  0.79846794  0.29490246  0.47661166 -0.51181482 -0.04116772 -0.10797632
[2,] -0.03540916          NA  0.47550478 -0.24284088 -0.01898357 -0.67102287 -0.46488410  0.01125144  0.13355919  0.08738474
[3,] -0.29183703  0.47550478          NA -0.05203104 -0.26311149  0.01120055 -0.16521411  0.49215496  0.40571893  0.30595246
[4,]  0.49358124 -0.24284088 -0.05203104          NA  0.60558581  0.53848638  0.80623397 -0.49950396 -0.01080598  0.41798727
[5,]  0.79846794 -0.01898357 -0.26311149  0.60558581          NA  0.33295170  0.53675545 -0.54756131  0.09225002 -0.01925587
[6,]  0.29490246 -0.67102287  0.01120055  0.53848638  0.33295170          NA  0.72936185  0.09463988  0.14607018  0.19487579
[7,]  0.47661166 -0.46488410 -0.16521411  0.80623397  0.53675545  0.72936185          NA -0.46348644 -0.05275132  0.47619940
[8,] -0.51181482  0.01125144  0.49215496 -0.49950396 -0.54756131  0.09463988 -0.46348644          NA  0.64924510  0.06783324
[9,] -0.04116772  0.13355919  0.40571893 -0.01080598  0.09225002  0.14607018 -0.05275132  0.64924510          NA  0.44698207
[10,] -0.10797632  0.08738474  0.30595246  0.41798727 -0.01925587  0.19487579  0.47619940  0.06783324  0.44698207          NA

最后執行:

   max(apply(cordf,2,min,na.rm=TRUE),na.rm = TRUE)#avoiding NA's 

一個人可以得到:

[1] -0.05275132

局部最小值的最大值。

編輯:

為了得到矩陣的索引

>which(cordf==max(apply(cordf,2,min,na.rm=TRUE),na.rm = TRUE))
[1]68 77 

或者為了得到坐標:

> which(cordf==max(apply(cordf,2,min,na.rm=TRUE),na.rm = TRUE), arr.ind = TRUE)
     row col
[1,]   8   7
[2,]   7   8

在我看來,在某些投影中你有空間點。 有人可能會爭辯說,距離其余部分最遠的點是離中心最遠的點(平均坐標):

library(raster)

set.seed(21)

# create fake points
coords <- data.frame(x=sample(438000:443000,10),y=sample(6695000:6700000,10))

# calculate center
center <- matrix(colMeans(coords),ncol=2)

# red = center, magenta = furthest point (Nr.2)
plot(coords)

# furthest point #2
ix <- which.max(pointDistance(coords,center,lonlat = F))

points(center,col='red',pch='*',cex=3)
points(coords[ix,],col='magenta',pch='*',cex=3)

segments(coords[ix,1],coords[ix,2],center[1,1],center[1,2],col='magenta')

在此輸入圖像描述

要找到離其余點最遠的點,你可以做這樣的事情。 當你說距離其他數據最遠的點時,我選擇了中值距離。 如果你有一組非常接近的點,那么中位數應該保持穩健。

可能還有一種方法可以通過層次聚類來實現這一目標,但目前它正在逃避我。

set.seed(1234)
mat <- rbind(matrix(rnorm(100), ncol=2), c(-5,5), c(-5.25,4.75))
d <- dist(mat)
sort(apply(as.matrix(d), 1, median), decreasing = T)[1:5]
# 51       52       20       12        4 
# 6.828322 6.797696 3.264315 2.806263 2.470919 

我寫了一個方便的小功能,你可以用它從最大的線距離中挑選。 您可以使用n參數指定是否需要最大,第二大等等。

getBigSegment <- function(x, y, n = 1){
  a <- cbind(x,y)
  d <- as.matrix(dist(a, method = "euclidean"))
  sorted <- order(d, decreasing = T)
  sub <- (1:length(d))[as.logical(1:length(sorted) %% 2)]
  s <- which(d == d[sorted[sub][n]], arr.ind = T)
  t(cbind(a[s[1],], a[s[2],]))
}

通過一些類似於您自己的示例數據,您可以看到:

set.seed(100)
mydata <- data.frame(x = runif(10, 438000, 445000) + rpois(10, 440000), 
                     y = runif(10, 6695000, 6699000) + rpois(10, 6996000))

# The function
getBigSegment(mydata$x, mydata$y)
#            x        y
#[1,] 883552.8 13699108
#[2,] 881338.8 13688458    

下面你可以看到我將如何使用這樣的功能

# easy plotting function
pointsegments <- function(z, ...) {
  segments(z[1,1], z[1,2], z[2,1], z[2,2], ...)
  points(z, pch = 16, col = c("blue", "red"))

}

plot(mydata$x, mydata$y) # points
top3 <- lapply(1:3, getBigSegment, x = mydata$x, y = mydata$y) # top3 longest lines
mycolors <- c("black","blue","green") # 3 colors
for(i in 1:3) pointsegments(top3[[i]], col = mycolors[i]) # plot lines
legend("topleft", legend = round(unlist(lapply(top3, dist))), lty = 1,
       col = mycolors, text.col = mycolors, cex = .8) # legend

在此輸入圖像描述

這種方法首先使用chull來識別extreme_points ,即位於給定點邊界上的點。 然后,對於每個extreme_points ,它通過排除該特定的extreme_points來計算extreme_pointscentroid 然后它從距離centroid最遠的extreme_points中選擇點。

foo = function(X = all_points){
    plot(X)
    chull_inds = chull(X)
    extreme_points = X[chull_inds,]
    points(extreme_points, pch = 19, col = "red")
    centroid = t(sapply(1:NROW(extreme_points), function(i)
        c(mean(extreme_points[-i,1]), mean(extreme_points[-i,2]))))
    distances = sapply(1:NROW(extreme_points), function(i)
        dist(rbind(extreme_points[i,], centroid[i,])))
    points(extreme_points[which.max(distances),], pch = 18, cex = 2)
    points(X[chull_inds[which.max(distances)],], cex = 5)
    return(X[chull_inds[which.max(distances)],])
}

set.seed(42)
all_points = data.frame(x = rnorm(25), y = rnorm(25))
foo(X = all_points)
#           x         y
#18 -2.656455 0.7581632

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM