Apply Max to each element of a list [duplicate]
Clash Royale CLAN TAG#URR8PPP
$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
map
$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.
add a comment |
$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
map
$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 notMax[#, 5] & /@ a
?
$endgroup$
– Mauro Lacy
Jan 3 at 11:38
add a comment |
$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
map
$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
map
edited Jan 3 at 10:24
Luca Amerio
asked Jan 3 at 10:20
Luca AmerioLuca Amerio
233
233
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 notMax[#, 5] & /@ a
?
$endgroup$
– Mauro Lacy
Jan 3 at 11:38
add a comment |
2
$begingroup$
Why notMax[#, 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
add a comment |
1 Answer
1
active
oldest
votes
$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
$endgroup$
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$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
$endgroup$
add a comment |
$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
$endgroup$
add a comment |
$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
$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
answered Jan 3 at 10:30
kglrkglr
179k9198410
179k9198410
add a comment |
add a comment |
2
$begingroup$
Why not
Max[#, 5] & /@ a
?$endgroup$
– Mauro Lacy
Jan 3 at 11:38