# Position of Nearest in sublist

I want to get the position of nearest value or the position of the value in a certain range. When I have the list, such as

list={{{200.2,10,20,8.1},{215.4,12,13,9}},{{105,11,23.2,6.5},{175,21,5.1,3},{214,23,18,4.1},{199,18,24.5,7.5}},{{209,8,11.1,3.4},{233,4.3,5,2},{188,21,30.2,9.1}}};


Question1: How to write the position of each sublist, whose first element is nearest to 213? I mean the position list of {215.4,12,13,9},{214,23,18,4.1}, and {209,8,11.1,3.4},

pos1={2,3,1}


or

pos2= {{1,2},{2,3},{3,1}}.


After extracting the position, I need to make a list of

list2={{215.4,12,13,9},{214,23,18,4.1},{209,8,11.1,3.4}}


If somebody know how to derive pos1(or pos2) and list2, please tell me. (Maybe my problem is that I cannot handle the sublist freely.)

Question2: How to write the position of each sublist, whose third element is between 17.5 and21? Though the problem is similar to Question1, but this time NOT Nearest, but in the certain range.

I want to get the position list of {200.2, 10, 20, 8.1} and {214,23,18,4.1}

pos3={1,3,NaN}


or

pos4={{1,1},{2,3},{3,NaN}}


Then I also want to get the list3,

list3={{200.2, 10, 20, 8.1},{214,23,18,4.1}}


How can I derive pos3 (or pos4) and list3?

I'm also interested in how to write list3 using the list of pos3 (or pos4).

Q1.

ClearAll[nFs]
nFs = Nearest[#[[All, 1]] -> "Index"] & /@ list;

Flatten @ Through @ nFs @ 213

 {2, 3, 1}

MapIndexed[{#2[[1]], #} &] @ %

 {{1, 2}, {2, 3}, {3, 1}}

Extract[list, %]

 {{215.4, 12, 13, 9}, {214, 23, 18, 4.1}, {209, 8, 11.1, 3.4}}


Q2.

Flatten[Values /@ KeySelect[17.5 <= #[[3]] <= 21 &] /@ PositionIndex /@ list /.
{} -> "NaN"]

{1, 3, "NaN"}

MapIndexed[{#2[[1]], #} &] @ %

 {{1, 1}, {2, 3}, {3, "NaN"}}

Extract[list, DeleteCases[{_, "NaN"}]@%]

 {{200.2, 10, 20, 8.1}, {214, 23, 18, 4.1}}

• Thank you so much to give me suggestions! Now I see I should learn about the usage of functions related to Index, such as "MapIndexed" and "Position Index". Also, thank you for editing my question in haste and telling me solution to not only current question but also to the previous one. The code above is very nice to understand the usage of @ and %, too, which I'm weak at. Thanks again! Jul 4, 2020 at 14:56
• @nancy, my pleasure. Thank you for the accept. And welcome to mma.se.
– kglr
Jul 4, 2020 at 15:01
• You are so kind! Thank you VERY much. Jul 4, 2020 at 15:11
• I wish I had known mma.se much earlier. I found this community just the day before yesterday, and until then, only I could do was to read book or Help page of Wolfram. People here are warm-hearted like you. Jul 4, 2020 at 15:30
(* Q1 - nearest first elements *)
nffirst = Nearest[#, DistanceFunction -> Function[{p, q}, Abs[First[p] - First[q]]]] & /@ list;
First[#[{213, 0, 0}]] & /@ nffirst

(* result: {{215.4, 12, 13, 9}, {214, 23, 18, 4.1}, {209, 8, 11.1, 3.4}} *)

(* Q2 - positions in the 3rd element *)
Flatten[Join[
Position[#, x_ /; 17.5 <= x[[3]] <= 21, 1, Heads -> False] & /@
list] /. {{} -> NaN}]

(* result: {1, 3, NaN} *)
(* you should probably replace NaN with something more sensible like {} or Missing[] *)

• Thank you very much for telling me a nice answer. It's very easy to understand, even for experimentalists like me. I'm happy to know how to get the list I want with the functions I often use. I want to follow your answers for other questions to learn more. Jul 4, 2020 at 15:09