Reputation: 175
I have a vector X which can potentially contain a very large number of entries and I want to find the maximal index i such that X[i] <= a*i/m (for some constants a,m) and I'd really rather not loop over the vector. I thought about using Position but I can't figure out how to make a suitable function that would take into account the indexes of the vector.
I forgot to mention that the entries of the vector will be sorted so that might help.
Help would be appreciated
Upvotes: 0
Views: 46
Reputation: 21532
I interpret your question (as of "right now" ) differently from ChinmayP, so maybe this:
foo <- which ( X/(1:(length(X)) < a/m)
rev(foo)[1]
That will give you the element of X
with the largest index i
(as in X[i]
) which meets your condition.
Upvotes: 2
Reputation: 17189
Following should give you what you want.
max(which(X <= a/m * seq_along(X)))
Looping backwards might still be better idea though, as you only want maximum index value.
for ( i in length(X):1 ) { if (X[i] <= a*i/m) break } ; i
When the loop breaks, i will contain your required max index.
Upvotes: 1