Displaying 1 result from an estimated 1 matches for "a_vec".
Did you mean:
b_vec
2011 Nov 03
5
Is it possible to vectorize/accelerate this?
...eneck. It's quite fast because of R and vectorizing, but it has a very slow for loop. The adjacent element of a vector (in terms of index number) depends conditionally on the former value of itself. Like a simple cumulating function (eg. cumsum) but with condition. Let's show me an example:
a_vec = rnorm(100)
b_vec = rep(0, 100)
b_vec[1]=a_vec[1]
for (i in 2:100){b_vec[i]=ifelse(abs(b_vec[i-1]+a_vec[i])>1, a_vec[i], b_vec[i-1]+a_vec[i])}
print(b_vec)
(The behaviour is like cumsum's, but when the value would excess 1.0 then it has another value from a_vec.)
Is it possible to make thi...