Apply Max to each element of a list [duplicate]
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
New contributor
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.
add a comment |
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
New contributor
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 notMax[#, 5] & /@ a
?
– Mauro Lacy
Jan 3 at 11:38
add a comment |
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
New contributor
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
New contributor
New contributor
edited Jan 3 at 10:24
New contributor
asked Jan 3 at 10:20
Luca Amerio
233
233
New contributor
New contributor
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 notMax[#, 5] & /@ a
?
– Mauro Lacy
Jan 3 at 11:38
add a comment |
2
Why notMax[#, 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
add a comment |
1 Answer
1
active
oldest
votes
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}
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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}
add a comment |
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}
add a comment |
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}
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
kglr
177k9198407
177k9198407
add a comment |
add a comment |
2
Why not
Max[#, 5] & /@ a
?– Mauro Lacy
Jan 3 at 11:38