Applying a lower bound threshold on a list

Applying a lower bound threshold on a list



Consider the following list


list=0,0,0,10^-18,10^-15,10^-12,10^-10,1,1



How can I apply a threshold on the list that will give a lower bound for the values. For example


ApplyThreshold[list,10^-12];



will yield an outcome of


10^-12,10^-12,10^-12,10^-12,10^-12,10^-12,10^-10,1,1



and


ApplyThreshold[list,10^-9];



will yield an outcome of


10^-9,10^-9,10^-9,10^-9,10^-9,10^-9,10^-9,1,1




3 Answers
3



You can use Clip, Ramp or Max:


Clip


Ramp


Max


Clip[list, 10^-9, ∞]



1/1000000000, 1/1000000000, 1/1000000000, 1/1000000000,
1/1000000000, 1/1000000000, 1/1000000000, 1, 1


Ramp[list - 10^-9] + 10^-9 === Max[#, 10^-9] & /@ list === %



True



Timings:


SeedRandom[1]
lst = RandomReal[1, 100000];

(r1 = Ramp[lst - 10^-9] + 10^-9 ;) // RepeatedTiming // First



0.00017


(r2 = Clip[lst , 10^-9, Infinity];) // RepeatedTiming // First



0.000221


(r3 = Max[#, 10^-9] & /@ lst ;) // RepeatedTiming // First



0.129


(r4 = Map[crit[#, 1*10^-12] &, lst ];) // RepeatedTiming // First (* from Alexei's answer*)



0.185


r1 == r2 == r3 == r4



True



Try the following. This si your list:


lst = 0, 0, 0, 10^-18, 10^-15, 10^-12, 10^-10, 1, 1



This function transforms any number to what you want:


crit[x_, y_] := If[x >= y, x, y];



This applies it to the list:


Map[crit[#, 1*10^-12] &, lst]

(* 1/1000000000000, 1/1000000000000, 1/1000000000000, 1/1000000000000,
1/1000000000000, 1/1000000000000, 1/10000000000, 1, 1 *)



Here is another example of yours:


Map[crit[#, 1*10^-9] &, lst]

(* 1/1000000000, 1/1000000000, 1/1000000000, 1/1000000000,
1/1000000000, 1/1000000000, 1/1000000000, 1, 1 *)



Have fun!



You can do this succinctly with the ReplaceAll (/.) and Condition (/;) operators:


ReplaceAll (/.)


Condition (/;)


list /. x_ /; x < 10^-12 -> 10^-12






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)