4
$\begingroup$

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}
$\endgroup$
0
8
$\begingroup$

You can use Clip, Ramp or 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

$\endgroup$
2
$\begingroup$

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!

$\endgroup$
1
$\begingroup$

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.