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 would to select the first valley of the list of coordinates below. It should be {3, 1}.

list = {{1, 7}, {2, 4}, {3, 1}, {4, 4}, {5, 9}, {6, 8}, {7, 3}, {8, 1}, {9, 4}, {10, 9}};

I thought it could be done like this

Select[list, #1[[2]] > #2[[2]] &, 1]

but that does not work. How can it be done then?

share|improve this question
    
No, its the second coordinate that has to be lower than the previous 2.-coordinate... –  Simon Lausen Apr 20 at 12:02

2 Answers 2

list = {{1, 7}, {2, 4}, {3, 1}, {4, 4}, {5, 9}, {6, 8}, {7, 3}, {8,  1},{9, 4}, {10, 9}}; 
Last@First@Split[list, Last[#1] > Last[#2] &]
(* {3,1} *)

or

Catch[Fold[If[#2[[2]] > #1[[2]], Throw[#1], #2] &, First@list, Rest@list]]
(* {3,1} *)

or

First@NestWhile[Rest, list, #2[[1, 2]] < #1[[1, 2]] &, 2, Infinity, -1]
(* {3,1} *)
share|improve this answer
    
This seems to work. Thank you! –  Simon Lausen Apr 20 at 12:03
    
But I do not understand why the option of comparison between the elements of the list is not possible in Select[] and Position[], but only in Sort[] and Split[] as you propose... –  Simon Lausen Apr 20 at 12:09
    
@SimonLausen, my pleasure. –  kguler Apr 20 at 12:09
    
For Select the selection test function takes a single argument; it is applied to each element sepearately to decide whether the element qualifies for selection. For Split, Sort, Union ... the test function takes two arguments. –  kguler Apr 20 at 12:16

FYI: Select examines only one element at a time. To use Select in the way you want, Partition the list into adjacent pairs:

list = {{1, 7}, {2, 4}, {3, 1}, {4, 4}, {5, 9}, {6, 8}, {7, 3}, {8, 1}, {9, 4}, {10, 9}};

Select[Partition[list, 2, 1], #[[2, 2]] > #[[1, 2]] &, 1][[1, 1]]
(* {3, 1} *)
share|improve this answer

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.