-
Notifications
You must be signed in to change notification settings - Fork 0
/
near point.Rmd
44 lines (38 loc) · 878 Bytes
/
near point.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
---
title: "°ª²Î§@·~"
output: html_document
---
```{r}
x=sample(1:100,50,rep=T)
y=sample(1:100,50,rep=T)
xx=cbind(x,y)
id=sample(1:50,3)
selectx=xx[id,]
newx=xx[-id,]
log=matrix(0,ncol=3,nrow=47)
for (i in 1:47){
for (j in 1:3){
log[i,j]=(selectx[j,1]-newx[i,1])^2+(selectx[j,2]-newx[i,2])^2
}
}
group=apply(log,1,which.min)
group1=newx[group==1,]
group2=newx[group==2,]
group3=newx[group==3,]
group1=rbind(group1,selectx[1,])
group2=rbind(group2,selectx[2,])
group3=rbind(group3,selectx[3,])
mean1=apply(group1,2,mean)
mean2=apply(group2,2,mean)
mean3=apply(group3,2,mean)
mean1=matrix(mean1,ncol=2)
mean2=matrix(mean2,ncol=2)
mean3=matrix(mean3,ncol=2)
library(FNN)
a1=get.knnx(group1,mean1,k=3,"kd_tree")
a2=get.knnx(group2,mean2,k=3,"kd_tree")
a3=get.knnx(group3,mean3,k=3,"kd_tree")
group1[a1$nn.index[1],]
group2[a2$nn.index[1],]
group3[a3$nn.index[1],]
```