提问人:sonia 提问时间:3/21/2023 最后编辑:sonia 更新时间:3/21/2023 访问量:16
测试两个矩阵重叠的角度区域
Test angle areas of two matrices on overlapping
问:
我在 R 中工作:
我确实有两个 data.frames,其中一列用于起始角度,一列用于角度间隔的结束角度,这是逆时针测量的。
data.frame baumtabelle(显示第 3 列至第 5 列)
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)```
答: 暂无答案
评论