测试两个矩阵重叠的角度区域

问题描述 投票:0回答:0

我在 R 工作:

我确实有两个数据框,其中一列用于起始角度,一列用于角度间隔的结束角度,这是逆时针测量的。

data.frame baumtabelle(显示第 3 至 5 列)

data.frame bereiche

data.frame baumtabelle 仍然有重叠的角度间隔,我在 data.frame bereiche 中删除了它,方法是将所有角度值保存在一个向量中并按值对它们进行排序,然后再次重新加载到 data.frame 中。 但是现在 bereiche 中有新的间隔,没有来自 data.frame baumtabelle 的任何“原始”间隔,我需要知道来自 baumtabelle 的哪些间隔与 bereiche 中的新间隔重叠。这些间隔数(在 data.frame baumtabelle 第一列 Nr_Baum 中)应该保存在 data.frame bareiche 第 3 列中——这在图片中显然是不正确的。

我不确定,如果我的重叠条件是错误的,或者如果将 baumtabelle 间隔的数量寻址到 bereiche 间隔有问题。

我在这里添加我的代码:

baumtabelle <- baumtabelle_neu <- dput(baumtabelle_neu)
structure(
list(
h = c(10, 15, 7, 20),
KM = c(7, 10, 6, 15),
Nr_Baum = c(1,
2, 3, 4),
alpha01 = c(0, 31.3669777746336, 22.6198649480404,
282.972239886185),
alpha02 = c(0, 58.6330222253664, 36.869897645844,
347.027760113815),
e = c(0, 8.48528137423857, 8.06225774829855,
5.65685424949238)
   ),
class = "data.frame",
row.names = c(NA, 4L)
 )

bereiche <- dput(bereiche)
structure(
list(
alpha01_neu = c(
22.6198649480404,
31.3669777746336,
36.869897645844,
58.6330222253664,
282.972239886185
     ),
alpha02_neu = c(
31.3669777746336,
36.869897645844,
58.6330222253664,
282.972239886185,
347.027760113815
     ),
Nr_Baum = c(NA, NA, 2, NA, NA),
relevant_Baum = c(4, 4, 2,
4, 4)
   ),
class = "data.frame",
row.names = c(NA,-5L)
 )

Define function to check for overlap
check_overlap <- function(start1, end1, start2, end2) {
Check if ranges overlap
return (((start1 < end2) & (end1 > start2)) | ((start1 == start2) & (end1 == end2)))
 }

for (i in 1:nrow(bereiche)) {
Initialize an empty vector to store the matching baumtabelle numbers
match_vec <- vector("character")

for (j in 1:nrow(baumtabelle)) {
check if any of the values in column 4 or 5 of baumtabelle_neu are between the values in column 2 and 3 of bereiche

if (check_overlap(baumtabelle[j,4], baumtabelle[j,5], bereiche[i,1], bereiche[i,2])){

if a match is found, append the number of baumtabelle column 3 to the match_vec
match_vec <- append(match_vec, as.character(baumtabelle[j, 3]))



Combine all the numbers in match_vec as a single string separated by commas
match_str <- paste(match_vec, collapse = " ")

Write the combined string to column 3 of bereiche
bereiche[i, 3] <- match_str
   }
 }

我想我可以这样解决问题:

for (i in 1:nrow(bereiche)) {
  overlapping_intervals <- c()
  for (j in 1:nrow(baumtabelle)) {
    if (baumtabelle[j,4] < bereiche[i,1]  && baumtabelle[j,5] > bereiche[i,1] ||
        baumtabelle[j,4] > bereiche[i,1]  && baumtabelle[j,4] < bereiche[i,2] ||
        baumtabelle[j,4] == bereiche[i,1] && baumtabelle[j,5] == bereiche[i,2] ||
        baumtabelle[j,4] == bereiche[i,1] && baumtabelle[j,5] > bereiche[i,2] ||
        baumtabelle_neu[j,4] < bereiche[i,1] && baumtabelle_neu[j,5] == bereiche[i,2]
        ) {
      overlapping_intervals <- append(overlapping_intervals, baumtabelle[j,3])
    }
  }
  bereiche[i,3] <- paste(overlapping_intervals, collapse = ",")
}
bereiche <- subset(bereiche, bereiche[,3] >=1)```
r comparison intervals angle
© www.soinside.com 2019 - 2024. All rights reserved.