# List transformation based on multiple conditions

Given a list of numerical values

a={0.02, 1.7, 0.25, 0.16, 0.24, 0.4, 0.46, 0.38, 0.21}


construct a corresponding list based on the set of criteria

if [ai] < 0.05 assign -> p1, 0.05 <= [ai]< 0.15 -> p2, 0.15 <= [ai]< 0.25 -> p3, 0.25 <= [ai]< 0.45 -> p4, [ai]=> 0.45 -> p5

The expected output would be a list b={p1,p5,p4,p3.....}

• How about constructing a Function with Piecewise and Mapping it over the list? Dec 24 '18 at 17:25
• Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! Dec 24 '18 at 17:38
• What is [ai]? Is it a[[i]]? Some familiarity with Mathematiica is expected, but this does not show much: is this a question about using the software system Mathematica? Dec 24 '18 at 17:40

f = x \[Function] Piecewise[{
{p1, x < 0.05},
{p2, 0.5 <= x < 0.15},
{p3, 0.15 <= x < 0.25},
{p4, 0.25 <= x < 0.45}
},
p5 (*default value*)
];

a = {0.02, 1.7, 0.25, 0.16, 0.24, 0.4, 0.46, 0.38, 0.21};
f /@ a


{p1, p5, p4, p3, p3, p4, p5, p4, p3}

• Thank you so much ! I am embarrassed to say how much time I was scratching my head. Dec 24 '18 at 18:43
• Michael - I fully agree with you. I just started...... Sorry for such fundamental error. Dec 24 '18 at 18:45
• @Sextus You're welcome. Dec 24 '18 at 21:08