1
$\begingroup$

I have list coordinates data points

{63.2802, 20.2614}, {61.5142, 24.8447}, {59.3288, 29.4583}, {56.9188, 33.6827}, {53.8731, 38.1929}, {51.5246, 41.1474}, {47.8597, 43.6472}, {47.6348, 40.8387}, {46.5147, 39.3611}, {45.7298, 37.748}

If I have a known coordinate, e.g.

{47.8597, 43.6472}

How to know the position of these known coordinate from the list?

$\endgroup$
2
$\begingroup$

As mention in the comment, you can use Position[expr,pattern] to locate,

list = {{63.2802, 20.2614}, {61.5142, 24.8447}, {59.3288, 29.4583}, {56.9188, 33.6827},
 {53.8731, 38.1929}, {51.5246, 41.1474}, {47.8597, 43.6472}, {47.6348, 40.8387}, {46.5147, 
       39.3611}, {45.7298, 37.748}};

Position[list, {47.8597, 43.6472}]

{{7}}

$\endgroup$
2
$\begingroup$
list = {{63.2802, 20.2614}, {61.5142, 24.8447}, {59.3288, 29.4583},                 
        {56.9188, 33.6827}, {53.8731, 38.1929}, {51.5246, 41.1474}, 
        {47.8597, 43.6472}, {47.6348, 40.8387}, {46.5147, 39.3611}, 
        {45.7298, 37.748}}

Nearest[list -> "Index", {47.8597, 43.6472}]

{7}

If you want to search for more indices:

Nearest[list -> "Index", {{47.8597, 43.6472}, {45.7298, 37.748}}]

{{7}, {10}}
$\endgroup$
  • $\begingroup$ I used Wolfram Mathematica 10.3, but It's not work in my computer. Do you have advice to me? $\endgroup$ – SelfA Jul 27 '17 at 2:47
  • 1
    $\begingroup$ @SelfA For Mathematica 10.3, use Nearest[list -> Range@Length@list, {47.8597, 43.6472}] $\endgroup$ – Carl Woll Jul 27 '17 at 5:37
  • $\begingroup$ @mrz It's work in my computer. $\endgroup$ – SelfA Jul 27 '17 at 11:24
1
$\begingroup$

If you have a long list of coordinates and you're frequently having to find positions, a faster approach might be to use PositionIndex to get an association between elements of a list and their positions:

coords = {{63.2802, 20.2614}, {61.5142, 24.8447}, {59.3288, 29.4583}, {56.9188, 33.6827}, {53.8731, 38.1929}, {51.5246, 41.1474}, {47.8597, 43.6472}, {47.6348, 40.8387}, {46.5147, 39.3611}, {45.7298, 37.748}};
indexassoc = PositionIndex[coords]

(* <|{63.2802, 20.2614} -> {1}, {61.5142, 24.8447} -> {2}, {59.3288, 29.4583} -> {3},  
     {56.9188, 33.6827} -> {4}, {53.8731, 38.1929} -> {5}, {51.5246, 41.1474} -> {6}, 
     {47.8597, 43.6472} -> {7}, {47.6348, 40.8387} -> {8}, {46.5147, 39.3611} -> {9}, 
     {45.7298, 37.748} -> {10}|> *)

Once you have that, you can easily find the position of any coordinate pair by, for example,

indexassoc[{47.8597, 43.6472}]
indexassoc[{61.5142, 24.8447}]

(* 
   {7}
   {2} 
*)

Update: PositionIndex works on 10.4 and above, but for 10.3 and lower you could build the association explicitly:

indexassoc = Association[#1 -> #2 & @@@ Transpose[{#, Range@Length@#}]] &@coords;
indexassoc[{47.8597, 43.6472}]
indexassoc[{61.5142, 24.8447}]

(* 
   7
   2
*)

Comparisons

While you do have the overheads of constructing the Association, if you have to search more than a few times, it quickly becomes worth it. Trying out some different methods on longer lists:

list = Join[RandomReal[{0, 50}, {999999, 2}], {{47.8597, 43.6472}}];

AbsoluteTiming@Nearest[list -> "Index", {47.8597, 43.6472}]
AbsoluteTiming@Position[list, {47.8597, 43.6472}]

(* {0.00471799, {1000000}}
   {0.514604, {{1000000}}} *)

AbsoluteTiming[indexassoc = PositionIndex[list];]
AbsoluteTiming@indexassoc[{47.8597, 43.6472}]

(* {2.48619, Null}  -- It takes a while to build 
   {7.15318*10^-6, 1000000}  -- but is several orders of magnitude faster once you have it *)
$\endgroup$
  • $\begingroup$ I used Wolfram Mathematica 10.3, but It's not work in my computer. Do you have advice to me?. Because any output mesage: Missing["KeyAbsent", {47.8597, 43.6472}] $\endgroup$ – SelfA Jul 27 '17 at 2:52
  • $\begingroup$ @SelfA See my edit. $\endgroup$ – aardvark2012 Jul 27 '17 at 3:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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