# Find the element in a list of triples which has z-coordinate nearest a specified value

Here is my list coordinate {x,y,z}

list  = {{30.8179, -62.2833, -155.032}, {33.1172, -61.0918, -156.656},
{35.3704, -59.8155, -158.28}, {37.5745, -58.456, -159.904},
{39.7264, -57.0155, -161.528}, {41.8232, -55.4957, -163.152},
{43.8619, -53.8989, -164.776}, {45.8397, -52.2272, -166.4}}


If I only knew the value of z coordinate only, e.g. 162.4 How to make programming in order to detect the nearest position or index with z value of 162.4?

• You've looked up Nearest[], no? – J. M. is in limbo Nov 3 '17 at 3:41
• Since the z-coordinates in your example data are all negative, I suspect you meant you want the triple having z closest to -164.4. Is that right? – m_goldberg Nov 3 '17 at 5:16
• You mean -162.4 right? – MCMastery Nov 3 '17 at 13:33

Assuming that you want to find the element of list that is nearest to z = -162.4, it can be done like so:

Extract[list, Nearest[list[[All, 3]] -> "Index", -162.4]]


{41.8232, -55.4957, -163.152}

• I've apply it in version 10.3 but it's not worked. Is there any additional code? – SelfA Nov 3 '17 at 6:21
• @Self, try Extract[list, Nearest[list[[All, 3]] -> Automatic, -162.4]]. – J. M. is in limbo Nov 3 '17 at 7:08
• Yeah, It works. – SelfA Nov 3 '17 at 7:11

To get the vector with the most similar z value:

Nearest[Last /@ list -> list, 162.4]
(* Out: {{30.8179, -62.2833, -155.032}} *)


To get the index of the same vector:

Nearest[Last /@ list -> Automatic, 162.4]
(* Out: {1} *)