1

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.

InquisitiveInquirer
  • 1,577
  • 1
  • 16
  • 28

2 Answers2

4

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

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
1
dr = {4, 7, 4, 8, 8, 5, 3, 4, 5, 4, 5, 5};
val = 4;
Flatten@Position[Thread[Less[dr, val]], True]
{7}
PhilChang
  • 598
  • 4
  • 11
  • Care to comment on how this approach differs (speed, efficiency, breadth, etc) from the currently accepted answer? – bobthechemist Apr 02 '14 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 02 '14 at 14:54