-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert-indices.r
45 lines (42 loc) · 1.78 KB
/
convert-indices.r
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
44
45
# TODO: Optimize these functions.
# Convert and absolute index to a part and relative index.
convert_indices <- function(lens, abs_indexes) {
index_ranges <- matrix(c(cumsum(lens)-lens+1, cumsum(lens)), ncol=2,
dimnames=list(NULL, c("start", "end")))
ret <- matrix(NA, nrow=length(abs_indexes), ncol=2,
dimnames=list(NULL, c("part", "index")))
for (i in 1:nrow(index_ranges)) {
ret_rows <- which(abs_indexes >= index_ranges[i, "start"] &
abs_indexes <= index_ranges[i, "end"])
ret[ret_rows, "part"] <- i
ret[ret_rows, "index"] <- abs_indexes[ret_rows]-index_ranges[i, "start"]+1
}
ret
}
convert_coord2d <- function(locs, i, j) {
ret <- matrix(NA, nrow=length(i) * length(j), ncol=5,
dimnames=list(NULL, c("i", "j", "part", "rel_i", "rel_j")))
ret[,"i"] <- rep(i, each=length(j))
ret[,"j"] <- j
for (k in 1:nrow(locs)) {
ret_rows <- which(ret[,"i"] >= locs[k,"start_rows"] &
ret[,"i"] <= locs[k,"end_rows"] &
ret[,"j"] >= locs[k,"start_cols"] &
ret[,"j"] <= locs[k,"end_cols"])
if (length(ret_rows) > 0) {
ret[ret_rows, "part"] <- k
ret[ret_rows, "rel_i"] <- ret[ret_rows, "i"] - locs[k,"start_rows"] + 1
ret[ret_rows, "rel_j"] <- ret[ret_rows, "j"] - locs[k,"start_cols"] + 1
}
}
# There is a better way to go from the i and j values in the
# matrix we're querying to the i and j values in the resulting matrix.
# However, it will take more time.
i_lookup <- 1:length(unique(ret[,"i"]))
names(i_lookup) <- as.character(unique(ret[,"i"]))
j_lookup <- 1:length(unique(ret[,"j"]))
names(j_lookup) <- as.character(unique(ret[,"j"]))
ret[,"i"] <- i_lookup[as.character(ret[,"i"])]
ret[,"j"] <- j_lookup[as.character(ret[,"j"])]
ret
}