0
$\begingroup$

I have a table of numbers with a few thousand entries, and I'm trying to find the first index that is less than a particular value. An example of the table I'm working on is as follows:

dr = {6.34152*10^8, 6.32076*10^8, 6.29998*10^8, 6.27921*10^8, 6.25843*10^8, 6.23765*10^8, ...}

What I'm trying to do is find the first entry that is less than 5.77*10^8. I've tried the following:

SOIdr = Position[dr, First[dr] < 5.77*10^8]

But have had no luck thus far. Any help would be appreciated, thanks guys.

$\endgroup$
3
$\begingroup$

This question is closely related to a number of others, if not an outright duplicate, but I can't find that duplicate now. Your syntax for using Position is not correct. You need something like this:

dr = {4, 7, 4, 8, 8, 5, 3, 4, 5, 4, 5, 5};
val = 4;

Position[dr, x_ /; x < val, 1, 1]
{{7}}

For numeric operations there are usually faster methods however. I am looking for a Q&A that gives some examples.


Related: Count consecutive occurrences in a list above a certain value

$\endgroup$
  • $\begingroup$ Thank you Mr. Wizard, worked like a charm. $\endgroup$ – InquisitiveInquirer Mar 17 '14 at 12:00
1
$\begingroup$
dr = {4, 7, 4, 8, 8, 5, 3, 4, 5, 4, 5, 5};
val = 4;
Flatten@Position[Thread[Less[dr, val]], True]
{7}
$\endgroup$
  • $\begingroup$ Care to comment on how this approach differs (speed, efficiency, breadth, etc) from the currently accepted answer? $\endgroup$ – bobthechemist Apr 2 '14 at 13:53
  • $\begingroup$ Under a very simple benchmark (1, 10, 100.. up to 1*10^6 repetitions), this answer has a smaller O(n) coefficient - 6.7s vs 9.1s for the worst case. But it finds all the smaller elements, not only the first. $\endgroup$ – Aisamu Apr 2 '14 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.