Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding first element of a Mathematica list greater than a threshold

I was wondering how I could obtain the first element of a (already ordered) list that is greater than a given threshold.

I don't know really well the list manipulation function in Mathematica, maybe someone can give me a trick to do that efficiently.

like image 453
Cedric H. Avatar asked Sep 08 '10 21:09

Cedric H.


1 Answers

Select does what you need, and will be consistent, respecting the pre-existing order of the list:

Select[list, # > threshold &, 1]

For example:

In[1]:= Select[{3, 5, 4, 1}, # > 3 &, 1]

Out[1]= {5}

You can provide whatever threshold or criterion function you need in the second argument.

The third argument specifies you only one (i.e., the first) element that matches.

Hope that helps!

like image 127
Michael Pilat Avatar answered Oct 03 '22 03:10

Michael Pilat