在R中找到相邻坐标的有效方法

时间:2017-07-13 17:07:26

标签: r algorithm matrix optimization

我正在使用具有X-Y坐标的矩阵数据集,其余列具有包含不同参数的逻辑值。我想找到X-Y的相邻坐标,给定至少一个相应的参数为真,然后将其作为行附加到新矩阵。以下是样本矩阵数据。

_1

  X  Y  P1 P2 P3 P4
-52  32  1  0  0  1
-50  34  0  0  0  0
-50  26  0  0  0  1
-52  31  0  1  1  1

要解决这个问题,我打算使用以下算法:

算法

# Find row wise sum
newCol <- rowSums(Data_1)

# Bind as first column with Data_1
newData <- cbind(newCol, Data_1)

# Not R code, pseduo code
if (newData[,1] != 0{
    store newData[,2] and newData[,3].
    Data_2 <- find neighboring coordinates to newData[,2] and newData[,3].
}

finalData <- cbind(Data_1, Data_2)

输出

  X  Y  P1 P2 P3 P4 N1.x N1.y N2.x N2.y N3.x N3.y N4.x N4.y N5.x N5.y N6.x N6.y N7.x N7.y N8.x N8.y
-52  32  1  0  0  1      <Neighboring Coordinates---->
-50  34  0  0  0  0      <NULL>
-50  26  0  0  0  1      <Neighboring Coordinates---->  
-52  31  0  1  1  1      <Neighboring Coordinates----> 

当矩阵具有数百万行和列时,这种方法的问题是可扩展性。

下图显示(x,y)的邻居坐标。

                     https://i.stack.imgur.com/ZbS84.gif

如果可能,请建议更好的方法,谢谢。

1 个答案:

答案 0 :(得分:1)

数据框方法怎么样 - 它需要是一个矩阵吗?

# Create one data frame with the starting points
points <- data.frame(x = c(-52, -50, -50, -52), 
                     y = c( 32,  34,  26,  31))

# Create a second data frame with the desired combinations of distances
distances <- expand.grid(xd = 1:4, 
                         yd = 1:4)

# Repeat the distances for each point (cartesian product/outer join)
neighbors <- merge(points, distances) 

# Compute neighbor coordinates
neighbors$nx <- neighbors$x + neighbors$xd
neighbors$ny <- neighbors$y + neighbors$yd

# sort
neighbors <- neighbors[order(neighbors$x, neighbors$y), ]

# display
head(neighbors)

结果

     x  y xd yd  nx ny
4  -52 31  1  1 -51 32
8  -52 31  2  1 -50 32
12 -52 31  3  1 -49 32
16 -52 31  4  1 -48 32
20 -52 31  1  2 -51 33
24 -52 31  2  2 -50 33