-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimalWarpPath.R
34 lines (30 loc) · 956 Bytes
/
optimalWarpPath.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
optimalWarpPath <- function(acc.cost.m) {
# Compute the optimal warp path over an accumulated cost matrix
i <- nrow(acc.cost.m)
j <- ncol(acc.cost.m)
# Optimal warping path
path <- list(c(i, j, acc.cost.m[i, j]))
while(i > 1 & j > 1) {
if(i == 2) {
j <- j - 1
} else if(j == 2) {
i <- i - 1
} else {
if(acc.cost.m[i - 1, j] == min(acc.cost.m[i - 1, j],
acc.cost.m[i, j - 1],
acc.cost.m[i - 1, j - 1])) {
i <- i - 1
} else if(acc.cost.m[i, j - 1] == min(acc.cost.m[i - 1, j],
acc.cost.m[i, j - 1],
acc.cost.m[i - 1, j - 1])) {
j <- j - 1
} else {
i <- i - 1
j <- j - 1
}
}
path <- c(path, list(c(i, j, acc.cost.m[i, j])))
}
path <- c(path, list(c(1, 1, 1)))
return(path)
}