How to find a smallest value in a list? [closed]












2














I have a list of point2d coordinate {x,y}. I would like to find the point where x is the smallest. How can I do it in Mathematica?










share|improve this question













closed as off-topic by Daniel Lichtblau, Bob Hanlon, m_goldberg, Henrik Schumacher, Niki Estner Jan 3 at 12:23


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Daniel Lichtblau, Bob Hanlon, m_goldberg, Henrik Schumacher, Niki Estner

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 3




    point2d[[Ordering[point2d, 1][[1]]]]
    – Henrik Schumacher
    Dec 31 '18 at 13:45






  • 7




    Take a look at MinimalBy
    – Kuba
    Dec 31 '18 at 13:47






  • 2




    First@Sort@point2d
    – Okkes Dulgerci
    Dec 31 '18 at 14:22
















2














I have a list of point2d coordinate {x,y}. I would like to find the point where x is the smallest. How can I do it in Mathematica?










share|improve this question













closed as off-topic by Daniel Lichtblau, Bob Hanlon, m_goldberg, Henrik Schumacher, Niki Estner Jan 3 at 12:23


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Daniel Lichtblau, Bob Hanlon, m_goldberg, Henrik Schumacher, Niki Estner

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 3




    point2d[[Ordering[point2d, 1][[1]]]]
    – Henrik Schumacher
    Dec 31 '18 at 13:45






  • 7




    Take a look at MinimalBy
    – Kuba
    Dec 31 '18 at 13:47






  • 2




    First@Sort@point2d
    – Okkes Dulgerci
    Dec 31 '18 at 14:22














2












2








2


1





I have a list of point2d coordinate {x,y}. I would like to find the point where x is the smallest. How can I do it in Mathematica?










share|improve this question













I have a list of point2d coordinate {x,y}. I would like to find the point where x is the smallest. How can I do it in Mathematica?







list-manipulation






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 31 '18 at 13:39









N.T.CN.T.C

43628




43628




closed as off-topic by Daniel Lichtblau, Bob Hanlon, m_goldberg, Henrik Schumacher, Niki Estner Jan 3 at 12:23


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Daniel Lichtblau, Bob Hanlon, m_goldberg, Henrik Schumacher, Niki Estner

If this question can be reworded to fit the rules in the help center, please edit the question.




closed as off-topic by Daniel Lichtblau, Bob Hanlon, m_goldberg, Henrik Schumacher, Niki Estner Jan 3 at 12:23


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Daniel Lichtblau, Bob Hanlon, m_goldberg, Henrik Schumacher, Niki Estner

If this question can be reworded to fit the rules in the help center, please edit the question.








  • 3




    point2d[[Ordering[point2d, 1][[1]]]]
    – Henrik Schumacher
    Dec 31 '18 at 13:45






  • 7




    Take a look at MinimalBy
    – Kuba
    Dec 31 '18 at 13:47






  • 2




    First@Sort@point2d
    – Okkes Dulgerci
    Dec 31 '18 at 14:22














  • 3




    point2d[[Ordering[point2d, 1][[1]]]]
    – Henrik Schumacher
    Dec 31 '18 at 13:45






  • 7




    Take a look at MinimalBy
    – Kuba
    Dec 31 '18 at 13:47






  • 2




    First@Sort@point2d
    – Okkes Dulgerci
    Dec 31 '18 at 14:22








3




3




point2d[[Ordering[point2d, 1][[1]]]]
– Henrik Schumacher
Dec 31 '18 at 13:45




point2d[[Ordering[point2d, 1][[1]]]]
– Henrik Schumacher
Dec 31 '18 at 13:45




7




7




Take a look at MinimalBy
– Kuba
Dec 31 '18 at 13:47




Take a look at MinimalBy
– Kuba
Dec 31 '18 at 13:47




2




2




First@Sort@point2d
– Okkes Dulgerci
Dec 31 '18 at 14:22




First@Sort@point2d
– Okkes Dulgerci
Dec 31 '18 at 14:22










1 Answer
1






active

oldest

votes


















7














Initializing some test data:



pts = RandomReal[100, {10, 2}];


Kuba's suggestion in the comments is this:



MinimalBy[pts, First]



{{31.2859, 50.9165}}




and here is a piece of code that captures the idea of Henrik's suggestion:



First@SortBy[pts, First]



{31.2859, 50.9165}




In other programming languages, we might have written a for loop and made a variable keep track of the smallest x coordinate so far. In Mathematica, we can also use loops but we try to stay away from it. If we were to implement that kind of solution in Mathematica, it would look like this instead:



min[pt1_, pt2_] := If[First[pt1] < First[pt2], pt1, pt2]
Fold[min, pts]



{31.2859, 50.9165}




Another pattern that one can see sometimes is to get the smallest value, get the position of the smallest value, and then extract that position from the original list:



minVal = pts[[All, 1]] // Min;
pos = Position[pts, {minVal, _}];
Extract[pts, pos]



{{31.2859, 50.9165}}




Note that I use Extract instead of Part to get the element, it's useful to remember that Extract works well with the type of output that Position gives. We could not have used Part to extract this information without first transforming the expression that Position gave.



I recommend the simplest approaches, i.e. one of the first two presented above. Most of all I recommend MinimalBy.






share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    7














    Initializing some test data:



    pts = RandomReal[100, {10, 2}];


    Kuba's suggestion in the comments is this:



    MinimalBy[pts, First]



    {{31.2859, 50.9165}}




    and here is a piece of code that captures the idea of Henrik's suggestion:



    First@SortBy[pts, First]



    {31.2859, 50.9165}




    In other programming languages, we might have written a for loop and made a variable keep track of the smallest x coordinate so far. In Mathematica, we can also use loops but we try to stay away from it. If we were to implement that kind of solution in Mathematica, it would look like this instead:



    min[pt1_, pt2_] := If[First[pt1] < First[pt2], pt1, pt2]
    Fold[min, pts]



    {31.2859, 50.9165}




    Another pattern that one can see sometimes is to get the smallest value, get the position of the smallest value, and then extract that position from the original list:



    minVal = pts[[All, 1]] // Min;
    pos = Position[pts, {minVal, _}];
    Extract[pts, pos]



    {{31.2859, 50.9165}}




    Note that I use Extract instead of Part to get the element, it's useful to remember that Extract works well with the type of output that Position gives. We could not have used Part to extract this information without first transforming the expression that Position gave.



    I recommend the simplest approaches, i.e. one of the first two presented above. Most of all I recommend MinimalBy.






    share|improve this answer




























      7














      Initializing some test data:



      pts = RandomReal[100, {10, 2}];


      Kuba's suggestion in the comments is this:



      MinimalBy[pts, First]



      {{31.2859, 50.9165}}




      and here is a piece of code that captures the idea of Henrik's suggestion:



      First@SortBy[pts, First]



      {31.2859, 50.9165}




      In other programming languages, we might have written a for loop and made a variable keep track of the smallest x coordinate so far. In Mathematica, we can also use loops but we try to stay away from it. If we were to implement that kind of solution in Mathematica, it would look like this instead:



      min[pt1_, pt2_] := If[First[pt1] < First[pt2], pt1, pt2]
      Fold[min, pts]



      {31.2859, 50.9165}




      Another pattern that one can see sometimes is to get the smallest value, get the position of the smallest value, and then extract that position from the original list:



      minVal = pts[[All, 1]] // Min;
      pos = Position[pts, {minVal, _}];
      Extract[pts, pos]



      {{31.2859, 50.9165}}




      Note that I use Extract instead of Part to get the element, it's useful to remember that Extract works well with the type of output that Position gives. We could not have used Part to extract this information without first transforming the expression that Position gave.



      I recommend the simplest approaches, i.e. one of the first two presented above. Most of all I recommend MinimalBy.






      share|improve this answer


























        7












        7








        7






        Initializing some test data:



        pts = RandomReal[100, {10, 2}];


        Kuba's suggestion in the comments is this:



        MinimalBy[pts, First]



        {{31.2859, 50.9165}}




        and here is a piece of code that captures the idea of Henrik's suggestion:



        First@SortBy[pts, First]



        {31.2859, 50.9165}




        In other programming languages, we might have written a for loop and made a variable keep track of the smallest x coordinate so far. In Mathematica, we can also use loops but we try to stay away from it. If we were to implement that kind of solution in Mathematica, it would look like this instead:



        min[pt1_, pt2_] := If[First[pt1] < First[pt2], pt1, pt2]
        Fold[min, pts]



        {31.2859, 50.9165}




        Another pattern that one can see sometimes is to get the smallest value, get the position of the smallest value, and then extract that position from the original list:



        minVal = pts[[All, 1]] // Min;
        pos = Position[pts, {minVal, _}];
        Extract[pts, pos]



        {{31.2859, 50.9165}}




        Note that I use Extract instead of Part to get the element, it's useful to remember that Extract works well with the type of output that Position gives. We could not have used Part to extract this information without first transforming the expression that Position gave.



        I recommend the simplest approaches, i.e. one of the first two presented above. Most of all I recommend MinimalBy.






        share|improve this answer














        Initializing some test data:



        pts = RandomReal[100, {10, 2}];


        Kuba's suggestion in the comments is this:



        MinimalBy[pts, First]



        {{31.2859, 50.9165}}




        and here is a piece of code that captures the idea of Henrik's suggestion:



        First@SortBy[pts, First]



        {31.2859, 50.9165}




        In other programming languages, we might have written a for loop and made a variable keep track of the smallest x coordinate so far. In Mathematica, we can also use loops but we try to stay away from it. If we were to implement that kind of solution in Mathematica, it would look like this instead:



        min[pt1_, pt2_] := If[First[pt1] < First[pt2], pt1, pt2]
        Fold[min, pts]



        {31.2859, 50.9165}




        Another pattern that one can see sometimes is to get the smallest value, get the position of the smallest value, and then extract that position from the original list:



        minVal = pts[[All, 1]] // Min;
        pos = Position[pts, {minVal, _}];
        Extract[pts, pos]



        {{31.2859, 50.9165}}




        Note that I use Extract instead of Part to get the element, it's useful to remember that Extract works well with the type of output that Position gives. We could not have used Part to extract this information without first transforming the expression that Position gave.



        I recommend the simplest approaches, i.e. one of the first two presented above. Most of all I recommend MinimalBy.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 31 '18 at 14:41

























        answered Dec 31 '18 at 14:15









        C. E.C. E.

        50.1k397202




        50.1k397202















            Popular posts from this blog

            Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

            ComboBox Display Member on multiple fields

            Is it possible to collect Nectar points via Trainline?