tags:

views:

97

answers:

2

Hello guys,

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.

Thanks !

+8  A: 

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!

Michael Pilat
That's exactly what I need, thanks !
Cedric H.
+2  A: 

You might want to look at TakeWhile[] and LengthWhile[] as well.

http://reference.wolfram.com/mathematica/ref/TakeWhile.html http://reference.wolfram.com/mathematica/ref/LengthWhile.html

Joshua Martell