Apply Max to each element of a list [duplicate]












4












$begingroup$



This question already has an answer here:




  • Applying a lower bound threshold on a list

    3 answers




I have a list



a = {1, 8, 0, 6, 5, 3, 5, 2, 2, 5}


I want to generate a new list whose elements are the same of a if it's bigger than 5, or 5 elsewhere.
I managed to achieve this using Map and a pure function doing



Map[(Max[#, 5]) &, a]


but this looks a bit clumsy to me. Is there a better way?



EDIT: I found this solution



a /. x_ /; x < 5 -> 5


but I cannot really understand why is working. Could someone give an insight into it?



Thanks










share|improve this question











$endgroup$



marked as duplicate by Kuba Jan 4 at 7:47


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 2




    $begingroup$
    Why not Max[#, 5] & /@ a ?
    $endgroup$
    – Mauro Lacy
    Jan 3 at 11:38
















4












$begingroup$



This question already has an answer here:




  • Applying a lower bound threshold on a list

    3 answers




I have a list



a = {1, 8, 0, 6, 5, 3, 5, 2, 2, 5}


I want to generate a new list whose elements are the same of a if it's bigger than 5, or 5 elsewhere.
I managed to achieve this using Map and a pure function doing



Map[(Max[#, 5]) &, a]


but this looks a bit clumsy to me. Is there a better way?



EDIT: I found this solution



a /. x_ /; x < 5 -> 5


but I cannot really understand why is working. Could someone give an insight into it?



Thanks










share|improve this question











$endgroup$



marked as duplicate by Kuba Jan 4 at 7:47


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 2




    $begingroup$
    Why not Max[#, 5] & /@ a ?
    $endgroup$
    – Mauro Lacy
    Jan 3 at 11:38














4












4








4


0



$begingroup$



This question already has an answer here:




  • Applying a lower bound threshold on a list

    3 answers




I have a list



a = {1, 8, 0, 6, 5, 3, 5, 2, 2, 5}


I want to generate a new list whose elements are the same of a if it's bigger than 5, or 5 elsewhere.
I managed to achieve this using Map and a pure function doing



Map[(Max[#, 5]) &, a]


but this looks a bit clumsy to me. Is there a better way?



EDIT: I found this solution



a /. x_ /; x < 5 -> 5


but I cannot really understand why is working. Could someone give an insight into it?



Thanks










share|improve this question











$endgroup$





This question already has an answer here:




  • Applying a lower bound threshold on a list

    3 answers




I have a list



a = {1, 8, 0, 6, 5, 3, 5, 2, 2, 5}


I want to generate a new list whose elements are the same of a if it's bigger than 5, or 5 elsewhere.
I managed to achieve this using Map and a pure function doing



Map[(Max[#, 5]) &, a]


but this looks a bit clumsy to me. Is there a better way?



EDIT: I found this solution



a /. x_ /; x < 5 -> 5


but I cannot really understand why is working. Could someone give an insight into it?



Thanks





This question already has an answer here:




  • Applying a lower bound threshold on a list

    3 answers








map






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 10:24







Luca Amerio

















asked Jan 3 at 10:20









Luca AmerioLuca Amerio

786




786




marked as duplicate by Kuba Jan 4 at 7:47


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Kuba Jan 4 at 7:47


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 2




    $begingroup$
    Why not Max[#, 5] & /@ a ?
    $endgroup$
    – Mauro Lacy
    Jan 3 at 11:38














  • 2




    $begingroup$
    Why not Max[#, 5] & /@ a ?
    $endgroup$
    – Mauro Lacy
    Jan 3 at 11:38








2




2




$begingroup$
Why not Max[#, 5] & /@ a ?
$endgroup$
– Mauro Lacy
Jan 3 at 11:38




$begingroup$
Why not Max[#, 5] & /@ a ?
$endgroup$
– Mauro Lacy
Jan 3 at 11:38










1 Answer
1






active

oldest

votes


















6












$begingroup$

You can use Clip or Ramp:



Clip[a, {5, ∞}]



{5, 8, 5, 6, 5, 5, 5, 5, 5, 5}




5 + Ramp[a - 5]



{5, 8, 5, 6, 5, 5, 5, 5, 5, 5}







share|improve this answer









$endgroup$




















    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6












    $begingroup$

    You can use Clip or Ramp:



    Clip[a, {5, ∞}]



    {5, 8, 5, 6, 5, 5, 5, 5, 5, 5}




    5 + Ramp[a - 5]



    {5, 8, 5, 6, 5, 5, 5, 5, 5, 5}







    share|improve this answer









    $endgroup$


















      6












      $begingroup$

      You can use Clip or Ramp:



      Clip[a, {5, ∞}]



      {5, 8, 5, 6, 5, 5, 5, 5, 5, 5}




      5 + Ramp[a - 5]



      {5, 8, 5, 6, 5, 5, 5, 5, 5, 5}







      share|improve this answer









      $endgroup$
















        6












        6








        6





        $begingroup$

        You can use Clip or Ramp:



        Clip[a, {5, ∞}]



        {5, 8, 5, 6, 5, 5, 5, 5, 5, 5}




        5 + Ramp[a - 5]



        {5, 8, 5, 6, 5, 5, 5, 5, 5, 5}







        share|improve this answer









        $endgroup$



        You can use Clip or Ramp:



        Clip[a, {5, ∞}]



        {5, 8, 5, 6, 5, 5, 5, 5, 5, 5}




        5 + Ramp[a - 5]



        {5, 8, 5, 6, 5, 5, 5, 5, 5, 5}








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 3 at 10:30









        kglrkglr

        188k10203421




        188k10203421















            Popular posts from this blog

            Bressuire

            Cabo Verde

            Gyllenstierna