# A problem with functions Flatten; Position; Nearest

Posted 9 years ago
4170 Views
|
3 Replies
|
2 Total Likes
|
 Hi all!I'm trying to find nearest to "a" elements of arrays "m5","m6" and take its indexes (in this arrays) for some computations (not connected with this problem).After some attempts, i find a solution: Print[ " PosNear5= ", Flatten[Position[m5, Flatten[Nearest[m5, a]][[1]]], 3], " PosNear6= ", Flatten[Position[m6, Flatten[Nearest[m6, a]][[1]]]][[1]]] ]the result is:PosNear5= {121} PosNear6= 108The second index is ok, but there is the question: Maybe, exist a more simple construction (in my variant - too many functions & brackets). (I want to see index without any figure brackets - for next step computations)...
3 Replies
Sort By:
Posted 9 years ago
 Try something like this (using Daniel's suggestion): nearestIndex[list_, a_] := Nearest[list -> Range[Length[list]], a][[1]]; {pos5 -> nearestIndex[m5, a], pos6 -> nearestIndex[m6, a]} 
Posted 9 years ago
 To get the position(s) you can use Nearest[values->Range[Length[values], a]. That should allow you to avoid Position. No idea why Flatten is needed without seeing your actual example.
Posted 9 years ago
 If you could edit your posting to include a small example with all the data, what the solution should be and why then it might be more likely that you would get good answers more quickly.