6
$\begingroup$

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?

$\endgroup$
3
  • $\begingroup$ You've looked up Nearest[], no? $\endgroup$ Commented Nov 3, 2017 at 3:41
  • $\begingroup$ 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? $\endgroup$
    – m_goldberg
    Commented Nov 3, 2017 at 5:16
  • $\begingroup$ You mean -162.4 right? $\endgroup$
    – MCMastery
    Commented Nov 3, 2017 at 13:33

2 Answers 2

8
$\begingroup$

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}

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

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} *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.