I have a vector
a = [1 1 1.01 1 1.05 1 ...
which is basically all numbers >=1. I also have a vector b that is the cummulative sum of this vector (and thus sorted), so each increment in this vector is at least 1 and sometimes (often) more than 1.
I also have a random number r between 0 and the total sum of a. Now, I need to find the first element k in b that is larger than r.
I am currently using a simple find comment:
However, I feel like it should be possible for this to be faster. Since I know that k should be at least floor(r) as increments in the cumsum vector are at least 1, I have tried
n = floor(r)
k = find(b(n:end)>r, 1) + n-1
But this seems to be slower despite skipping searching the first n enties of b. Is there an build-in option in matlab for starting the search at a certain index? If not, is there any other faster way? This function takes about 65% of my total computing time. a Is a vector of size 10.000 but it changes very often and I have to make this call millions of times.