rra*_*ral 9 interpolation r matrix igraph
我正在研究基于最小距离的两个坐标的矩阵的完成过程(我不确定正确的术语),让我解释一下。
我有一个 nxm(行和列)矩阵,NA 值和 1,见图 1:
目标是:
假设我找到了极端坐标(a和b见图 2),我正在尝试使用直线的向量方程:
所以我试图创建一个使用极值(a 和 b)和常数 k 的函数(见图 2)
completa <- function(a, b, k){
x <- y <- NULL
resta <- b - a
u_ba <- resta / sqrt(sum(resta^2))
for (i in seq(0, 1, k)) {
posi <- a + i * u_ba
x <- c(x, posi[1])
y <- c(y, posi[2])
}
coordenadas <- round(cbind(x, y))
return(coordenadas)
}
Run Code Online (Sandbox Code Playgroud)
示例矩阵在:
data_mat <- read.csv("https://www.dropbox.com/s/hz42scjuf9uib9y/data_test.csv?dl=1")
a <- c(25, 6)
b <- c(20, 10)
Run Code Online (Sandbox Code Playgroud)
当使用坐标为 a、b 和 k = 0.5(k 的值可以在 0 和 1 之间变化)的函数时,得到以下结果:
completa(a,b,0.5)
# x y
#[1,] 25 6
#[2,] 25 6
#[3,] 24 7
Run Code Online (Sandbox Code Playgroud)
但预期的输出是:
# x y
#[1,] 25 6
#[2,] 24 7
#[3,] 23 8
#[4,] 22 9
#[5,] 21 10 # or 21 9,
#[6,] 20 10
Run Code Online (Sandbox Code Playgroud)
很明显,这条线有不止一种解决方案,因此,建议最好考虑最小距离。
最后,在获得这些坐标后,只需为它们分配一个等于 1 的值即可。主要思想是使这个过程递归。并且最终矩阵的所有坐标都可以连接。
欢迎大家提出建议,谢谢。
据我所知,你提出了一个数学问题。当我理解正确时,您想要“击中”极端坐标之间的方块,以在 2 个簇之间建立一座桥梁。您所犯的错误是在 for 循环中。您在循环末尾仅添加了 ab 单位向量的 1 倍,因此您在网格中仅移动了 1 的距离。我已经以这种方式更正了您的代码,使其传输了完整的距离。我希望它能解决您的问题:
completa <- function(a, b, k){
if(k!=0){
x <- y <- NULL
resta <- b - a
vector_length = sqrt(sum(resta^2))
for (i in seq(0, 1, length.out=(vector_length/k))) {
posi <- a + i * resta
x <- c(x, posi[1])
y <- c(y, posi[2])
}
coordenadas <- round(cbind(x, y))
coordenadas <- unique(coordenadas[,1:2])
}
if(k==0) coordenadas = a
return(coordenadas)
}
Run Code Online (Sandbox Code Playgroud)
结果是
> completa(a,b,0.5)
x y
[1,] 25 6
[2,] 24 7
[3,] 23 7
[4,] 23 8
[5,] 22 8
[6,] 22 9
[7,] 21 9
[8,] 20 10
Run Code Online (Sandbox Code Playgroud)