2
$\begingroup$

Rip01 is a list of numbers. It's length is 128. PercentualeTaglio is a number. I would like to create a list which selects from Rip01 just the numbers greater than PercentualeTaglio and put a zero instead of all the numbers smaller than PercentualeTaglio. I wrote this code:

a = Table[If[Rip01[[k]] > PercentualeTaglio, Rip01[[k]], 0], {k, 1, 128}]

But when I evaluate a = Rip01. If I try to put "<" instead of ">":

a = Table[If[Rip01[[k]] < PercentualeTaglio, Rip01[[k]], 0], {k, 1, 128}]

I get a list of all zeros. Can anyone tell me what I am doing wrong?

$\endgroup$
  • $\begingroup$ I meant, when I evaluate I get a=Rip01. $\endgroup$ – Giulia ZH Oct 27 '17 at 15:57
  • 1
    $\begingroup$ Use Chop... Chop[Rip01, PercentualeTaglio] to accomplish this task. $\endgroup$ – bill s Oct 27 '17 at 16:40
  • $\begingroup$ Thank you very much! I did not know about Chop. Anyway, I would still like to know why my code did not work... @Anjar Kumar. Rip01 is just a list of measures I took. $\endgroup$ – Giulia ZH Oct 27 '17 at 17:27
  • $\begingroup$ probably PercentualeTaglio has a value smaller than the smallest value in your list; if every element in your list is greater than the value in PercentualeTaglio then If returns 0 as instructed $\endgroup$ – user42582 Oct 27 '17 at 19:44
  • $\begingroup$ also, you could try Thread[head[Rip01,PercentualeTaglio]]/.head[x_,y_]:>If[Greater[x,y],x,0.] in order to obtain the first table in the question; replace Greater with Less to obtain the second table in the question; using Table to access indices for Part is not something you'd generally want to do $\endgroup$ – user42582 Oct 27 '17 at 19:57
2
$\begingroup$

Here are other solutions:

SeedRandom[42]; 
Rip01 = RandomInteger[99, 128];
PercentualeTaglio = 50;

You can use:

Clip[Rip01, {PercentualeTaglio, Max@Rip01}, {0, 0}]

or:

Rip01 /. t_ /; t < PercentualeTaglio -> 0

or:

If[# > PercentualeTaglio, #, 0] & /@ Rip01

Result is always:

{
54, 66, 0, 0, 0, 0, 95, 0, 0, 0, 81, 87, 0, 88, 98, 0, 
0, 0, 71, 0, 66, 66, 98, 0, 0, 0, 0, 0, 83, 78, 68, 65, 
82, 64, 0, 63, 98, 0, 60, 0, 60, 0, 0, 0, 53, 0, 0, 78, 
69, 0, 0, 99, 60, 0, 81, 0, 77, 69, 0, 0, 0, 83, 0, 50, 
0, 78, 0, 0, 0, 0, 91, 0, 72, 0, 86, 0, 58, 64, 66, 64, 
0, 65, 0, 0, 0, 88, 78, 82, 0, 68, 97, 61, 0, 53, 81, 0, 
0, 0, 0, 96, 99, 0, 81, 64, 73, 0, 96, 89, 0, 0, 58, 65, 
58, 0, 0, 0, 78, 0, 0, 0, 77, 94, 0, 60, 0, 90, 0, 56
}
$\endgroup$
1
$\begingroup$

There a lots of ways of performing the task you want to do. Here is a functional approach based on the built-in function Select

You don't supply any sample data, so I contrive some.

SeedRandom[42]; data = RandomInteger[99, 128]
{54, 66, 4, 15, 6, 3, 95, 32, 48, 3, 81, 87, 22, 88, 98, 48, 44, 36, 71,  
 46, 66, 66, 98, 35, 41, 36, 22, 28, 83, 78, 68, 65, 82, 64, 24,63, 98, 
 47, 60, 8, 60, 32, 17, 26, 53, 26, 20, 78, 69, 25, 36, 99, 60, 21, 81, 
 31, 77, 69, 8, 36, 3, 83, 1, 50, 14, 78, 25, 32, 22, 25, 91, 1, 72, 40, 
 86, 4, 58, 64, 66, 64, 20, 65, 15, 26, 35, 88, 78, 82, 20, 68, 97, 61, 
 30, 53, 81, 1, 29, 18, 9, 96, 99, 26, 81, 64, 73, 37, 96, 89, 43, 18, 
 58, 65, 58, 22, 44, 28, 78, 35, 49, 4, 77, 94, 7, 60, 9, 90, 14, 56}

Now I define a function that given a threshold and a relationship to apply, returns a function that can select the numbers in data that satisfy the relationship with respect to the threshold.

choose[threshold_, func_] := Select[func[#, threshold] &]

Setting the threshold at 50, this function can split the data into those numbers > 50 and ≤ 50 like so

choose[50, Greater] @ data
{54, 66, 95, 81, 87, 88, 98, 71, 66, 66, 98, 83, 78, 68, 65, 
 82, 64, 63, 98, 60, 60, 53, 78, 69, 99, 60, 81, 77, 69, 83, 78, 
 91, 72, 86, 58, 64, 66, 64, 65, 88, 78, 82, 68, 97, 61, 53, 81, 
 96, 99, 81, 64, 73, 96, 89, 58, 65, 58, 78, 77, 94, 60, 90, 56}
choose[50, LessEqual] @ data
 {4, 15, 6, 3, 32, 48, 3, 22, 48, 44, 36, 46, 35, 41, 36, 22, 28, 24, 47, 8, 32, 
  17, 26, 26, 20, 25, 36, 21, 31, 8, 36, 3, 1, 50, 14, 25, 32, 22, 25, 1, 40, 4, 20, 15, 
  26, 35, 20, 30, 1, 29, 18, 9, 26, 37, 43, 18, 22, 44, 28, 35, 49, 4, 7, 9, 14}
$\endgroup$
1
$\begingroup$

Using m_goldberg's and mrz's example setup:

Rip01 UnitStep[Rip01 - PercentualeTaglio]

{54, 66, 0, 0, 0, 0, 95, 0, 0, 0, 81, 87, 0, 88, 98, 0, 0, 0, 71, 0, 66, 66, 98,
0, 0, 0, 0, 0, 83, 78, 68, 65, 82, 64, 0, 63, 98, 0, 60, 0, 60, 0, 0, 0, 53, 0, 0,
78, 69, 0, 0, 99, 60, 0, 81, 0, 77, 69, 0, 0, 0, 83, 0, 50, 0, 78, 0, 0, 0, 0, 91,
0, 72, 0, 86, 0, 58, 64, 66, 64, 0, 65, 0, 0, 0, 88, 78, 82, 0, 68, 97, 61, 0, 53,
81, 0, 0, 0, 0, 96, 99, 0, 81, 64, 73, 0, 96, 89, 0, 0, 58, 65, 58, 0, 0, 0, 78, 0,
0, 0, 77, 94, 0, 60, 0, 90, 0, 56}

$\endgroup$

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