Apply Max to each element of a list [duplicate]












4















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









New contributor




Luca Amerio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











marked as duplicate by Kuba 2 days ago


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




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
















4















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









New contributor




Luca Amerio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











marked as duplicate by Kuba 2 days ago


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




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














4












4








4


0






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









New contributor




Luca Amerio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












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









New contributor




Luca Amerio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Luca Amerio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Jan 3 at 10:24





















New contributor




Luca Amerio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Jan 3 at 10:20









Luca Amerio

233




233




New contributor




Luca Amerio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Luca Amerio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Luca Amerio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




marked as duplicate by Kuba 2 days ago


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 2 days ago


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




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














  • 2




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








2




2




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




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










1 Answer
1






active

oldest

votes


















6














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




























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    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


























      6














      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
























        6












        6








        6






        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












        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









        kglr

        177k9198407




        177k9198407















            Popular posts from this blog

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            RAC Tourist Trophy