# Picking minimum from a list?

Consider the following table where each point is in three dimension (x,y,z).

  tab={{{1, 1, 0.6}, {1, 2, 0.4}, {1, 3, 0.2}, {1, 4, 0.1}, {1, 5, 0.2}, {1,
6, 0.4}, {1, 7, 0.6}, {1, 8, 0.7}}, {{2, 1, 0.6}, {2, 2, 0.4}, {2,
3, 0.2}, {2, 4, 0.2}, {2, 5, 0.3}, {2, 6, 0.1}, {2, 7, 0.6}, {2,
8, 0.8}}, {{3, 1, 0.6}, {3, 2, 0.4}, {3, 3, 0.1}, {3, 4, 0.3}, {3,
5, 0.2}, {3, 6, 0.4}, {3, 7, 0.6}, {3, 8, 0.7}}, {{4, 1, 0.6}, {4,
2, 0.4}, {4, 3, 0.2}, {4, 4, 0.6}, {4, 5, 0.2}, {4, 6, 0.4}, {4, 7,
0.1}, {4, 8, 0.9}}, {{5, 1, 0.6}, {5, 2, 0.4}, {5, 3, 0.2}, {5, 4,
0.1}, {5, 5, 0.2}, {5, 6, 0.4}, {5, 7, 0.6}, {5, 8, 0.7}}, {{6,
1, 0.1}, {6, 2, 0.4}, {6, 3, 0.2}, {6, 4, 0.6}, {6, 5, 0.2}, {6, 6,
0.4}, {6, 7, 0.6}, {6, 8, 0.7}}}


In each row there an absolute minimum "0.1" corresponding to z, but it is located at random position in each row e.g. in the first row it is located in {1, 4, 0.1}, in the 2nd row it located at {2, 6, 0.1} and so on. Is there is any way to pick only those points with the minimum corresponding to z from all over the table ? Moreover, how to pick only the x and y corresponding to each minimum value "0.1" in each row. I know the command Min[], but that only works for one dimensional table. Could any one please have a look at this?

• TakeSmallestBy can be used for this. In[625]:= Map[TakeSmallestBy[#, #[[3]] &, 1][[1, 1 ;; 2]] &, tab] Out[625]= {{1, 4}, {2, 6}, {3, 3}, {4, 7}, {5, 4}, {6, 1}} May 5, 2021 at 22:55

Try

Map[ MinimalBy[#, Last] &, tab]
(*{{{1, 4, 0.1}}, {{2, 6, 0.1}}, {{3, 3, 0.1}}, {{4,7, 0.1}}, {{5, 4,0.1}}, {{6, 1, 0.1}}}*)

• Thanks a lot! It works! But one further step is also needed. How to express the output now in the form: ({{1, 4}, {2, 6}, {3, 3}, {4,7}, {5, 4}, {6, 1}}). It would be great if you look into it. One way is to use tab1= Flatten[Map[ MinimalBy[#, Last] &, tab]] and then call the first and 2nd elements in separate tables. But I wonder if you suggest any shortcut way which works in a single step in addition to the one you suggested above? Thanks! May 7, 2021 at 2:46
• Perhaps (Map[MinimalBy[#, Last] &, tab] // Flatten [#, 1] &)[[All, {1, 2}]] May 7, 2021 at 6:13
• Works very well, thanks a lot! One further step is needed, please. How the second element of the output above is now added by a constant value say "20" using the same line of the code: (Map[MinimalBy[#, Last] &, tab] // Flatten [#, 1] &)[[All, {1, 2}]] so that the final out now is: {{1, 24}, {2, 26}, {3, 23}, {4,27}, {5, 24}, {6, 21}}. If its possible, please also comment on this. Thanks! May 9, 2021 at 22:44
• I would prefer an extra line Map[# + {0, 20} &, ...]. Alternatively Map[Function[uu, uu + {0, 20}], (Map[MinimalBy[#, Last] &, tab] // Flatten[#, 1] &)[[All, {1, 2}]]] May 10, 2021 at 6:32
• @Thanks Ulrich Neumann! May 18, 2021 at 19:16

Try this:

Flatten[tab /. {x_, y_, z_} /; z != 0.1 -> Nothing, 1]

(* {{1, 4, 0.1}, {2, 6, 0.1}, {3, 3, 0.1}, {4, 7, 0.1}, {5, 4, 0.1}, {6,
1, 0.1}}  *)


or this:

Flatten[Map[Select[#, (#[[3]] == 0.1 &)] &, tab, {1}], 1]

(* {{1, 4, 0.1}, {2, 6, 0.1}, {3, 3, 0.1}, {4, 7, 0.1}, {5, 4, 0.1}, {6,
1, 0.1}}  *)


Have fun!

• Sorry, this minimum 0.1 is just an example and not necessarily be exactly this. It could different in each row. In which case, in a very long list, its not possible to pick each one manually like this. My problem is exactly about that. May 5, 2021 at 23:05
• Then you should precisely formulate what do you want and give corresponding examples. May 6, 2021 at 12:17
• Thanks, Alexei, please see the answer 1 above by Ulrich. May 7, 2021 at 2:48