Minimum across cumulative sums with different starting indices
Question: Given a vector, I want to know the minimum of a series of
cumulative sums, where each cumulative sum is calculated for an increasing
starting index of the vector and a fixed ending index (1:5, 2:5, ...,
5:5). Specifically, I am wondering if this can be calculated w/o using a
for() loop, and if there is potentially a term for this algorithm/
calculation. I am working in R.
Context: The vector of interest contains a time series of pressure
changes. I want to know of the largest (or smallest) net change in
pressure across a range of starting points but with a fixed end point.
Details + Example:
#Example R code
diffP <- c(0, -1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, -1, 0, 0, 0,
0, 0, 0, 0, -1, 0, 0)
minNet1 <- min(cumsum(diffP))
minNet1 #over the whole vector, the "biggest net drop" (largest magnitude
with negative sign) is -1.
#However, if I started a cumulative sum in the second half of diffP, I
would get a net pressure change of -2.
hold <- list()
nDiff <- length(diffP)
for(j in 1:nDiff){
hold[[j]] <- cumsum(diffP[j:nDiff])
}
answer <- min(unlist(hold)) #this gives the answer that I ultimately want
Hopefully my example above has helped to articulate my question. answer
contains the correct answer, but I'd rather do this without a for() loop
in R. Is there a better way to do this calculation, or maybe a name I can
put to it?
No comments:
Post a Comment