0
$\begingroup$

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.....}

$\endgroup$
3
  • 1
    $\begingroup$ How about constructing a Function with Piecewise and Mapping it over the list? $\endgroup$ Dec 24 '18 at 17:25
  • $\begingroup$ 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! $\endgroup$
    – Michael E2
    Dec 24 '18 at 17:38
  • $\begingroup$ 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? $\endgroup$
    – Michael E2
    Dec 24 '18 at 17:40
0
$\begingroup$
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}

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

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.