Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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

share|improve this answer
    
Thank you Mr. Wizard, worked like a charm. –  user7388 Mar 17 at 12:00
dr = {4, 7, 4, 8, 8, 5, 3, 4, 5, 4, 5, 5};
val = 4;
Flatten@Position[Thread[Less[dr, val]], True]
{7}
share|improve this answer
    
Care to comment on how this approach differs (speed, efficiency, breadth, etc) from the currently accepted answer? –  bobthechemist Apr 2 at 13:53
    
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. –  Aisamu Apr 2 at 14:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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