4

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

Luca
  • 383
  • 1
  • 8

1 Answers1

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}

kglr
  • 394,356
  • 18
  • 477
  • 896