2

I have some data that is two dimensional (traditional (x, y) format) as so:

var = {{x1, y1}, {x2, y2}, {x3, y3}}

I would like to search through that large list and find the maximum and minimum y values and return the ordered pair. Something like:

min[var]->{x2, y2} //read "->" as returns {the minimum ordered pair}
max[var]->{x3, y3} //read "->" as returns {the maximum ordered pair}

Finally I know the data has a "root". A plot of the points cross the x-axis. I would like to find the closest few (maybe 3 or 4) points to zero. Questions similar to this have been posted but I cannot figure out how to adapt those solutions to my needs

previous work 1

previous work 2

Matthew Kemnetz
  • 809
  • 7
  • 13

2 Answers2

2

You can define your own function with which to sort a list. For instance:

tab=Table[{Random[] - 0.5, Random[] - 0.5}, {n, 20}]
sorted=Sort[tab,#2[[1]]^2>#1[[1]]^2&]

This will give you a list where the elements with x closest to 0 will be first. You can take the number of desired values from this list.

For the element with the highest and lowest y element, you can use @Sjoerd C. de Vries' example in the comment, or you can define your own ordering:

sortedminmaxy=Sort[tab,#2[[2]]>#1[[2]]&]

and this will have the data ordered by the y value. You can then take the first and last elements as you wish.

Jonathan Shock
  • 3,015
  • 15
  • 24
0

Using the pattern I described here, define

MaxBy[list_, fun_] := list[[First@Ordering[fun /@ list, -1]]]
MinBy[list_, fun_] := list[[First@Ordering[fun /@ list, 1]]]

then do

MinBy[data, Last] (* smallest y *)
MaxBy[data, Last] (* largest y *)
MinBy[data, Composition[Abs, Last] ] (* closest to 0 *)
Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263