zz is a time series, with the function I am trying to find those elements where the time series becames greater than 2 or smaller than -2. For instance, in a series of 1,3,5 , I am interested only in the second element, not in the third. If I have that, then I substract the previous element from this. I suppose both the for loop and the way I am adding this values to a vector one-by-on makes the code considerably slower. Could someone suggest me a way to avoid them? Thank you,
for loops have their place even though they have a reputation for being slow, which can happen if interim steps aren't written to a pre-allocated object, etc. In many of the cases in which users coming from a procedural language background, the simpler functional approach does not readily come to mind.
In this case, having the indices of the elements of zz that satisfies the conditions provides a step to further processing.