I have a list of coordinates like this one:
{{x1,y1},{x2,y2},{x3,y3},...,{xn,yn}}
I need to get the minimum and the maximum of all x-values and the minimum and maximum of all y-values. Is this possible? If I use the in-built Min function for example gives me just the minimum of all x and y values...
Answer
Well, you could extract the y values and then calculate their minimum.
rand = RandomInteger[{0, 9}, {10, 3}]
{{6, 5, 2}, {3, 8, 3}, {0, 4, 0}, {3, 7, 5}, {4, 2, 7}, {6, 4, 5}, {3, 3, 3}, {7, 9, 7}, {4, 5, 5}, {4, 2, 5}}
Min[ rand[[All, 2]] ] (* 1 = x, 2 = y, 3 = z *)
Max[ rand[[All, 2]] ]
2
9
Another approach is of course sorting the list by user-defined rules and then picking the first/last element. For example, the following sorts the list according to ascending y values, and then extracts the min/max from that.
sorted = Sort[rand, #1[[2]] < #2[[2]] &]
sorted[[1, 2]] (* Minimal y *)
sorted[[-1, 2]] (* Maximal y *)
2
9
Note that this actually sorts the whole list, which is far less effective computationally. Normally, min/max functions don't sort, they just search.
Comments
Post a Comment