3
$\begingroup$

I'm so sorry that this is probably a basic question. I am a student and have 0 prior experience with Mathematica or any sort of coding and I have looked at various resources trying to figure it out myself but still need help.

I currently have a list of values (called "ProbSurvMale" in my code)

{{0.958598}, {0.694663}, {0.118085}, {0.390693}, {0.762198}, \
{1.55629*10^-6}, {0.0292393}, {0.320323}, {0.139982}, {0.809311}, \
{0.0993175}, {0.961641}, {0.033014}, {0.833012}, {0.125618}, \
{0.971254}, {0.000109839}, {0.116335}, {0.00260359}, {0.0837473}}

I also have a RandomReal function.

draw := RandomReal[{0, 1}]

My code for the If statement is

Contest := If[draw < x, "live", "ded"]

I want to run the If statement for every value in the ProbSurvMale list. I don't know how to apply it to the list. I tried Contest @@@ ProbSurvMale, which gave me

{"live"[0.00206366], "live"[0.000243393], "live"[0.118085], 
 "live"[0.390693], "live"[0.0451756], "live"[0.44083], 
 "live"[0.851076], "live"[0.0000135539], "live"[1.28328*10^-6], 
 "live"[0.0362936], "live"[0.560129], "live"[0.0130393], 
 "live"[0.000628627], "live"[0.000651911], "live"[0.493252], 
 "live"[0.0116439], "live"[0.579737], "live"[0.0000690552], 
 "live"[0.0110516], "live"[0.0837473]}

and I tried Map[Contest, {ProbSurvMale}], which gave me

{"live"[{{0.00206366}, {0.000243393}, {0.118085}, {0.390693}, \
{0.0451756}, {0.44083}, {0.851076}, {0.0000135539}, {1.28328*10^-6}, \
{0.0362936}, {0.560129}, {0.0130393}, {0.000628627}, {0.000651911}, \
{0.493252}, {0.0116439}, {0.579737}, {0.0000690552}, {0.0110516}, \
{0.0837473}}]}

Also, "live" is a placeholder for now. I want the term to retain its original value if the If condition is true.

Any help would be greatly appreciated!

$\endgroup$

1 Answer 1

2
$\begingroup$
Contest[x_] := If[draw <x, "live", "ded"];
Map[Contest, ProbSurvMale, {-1}]

{{"live"}, {"live"}, {"ded"}, {"live"}, {"ded"}, {"ded"}, {"ded"}, {"ded"}, {"ded"}, {"live"}, {"ded"}, {"live"}, {"ded"}, {"ded"}, {"ded"}, {"live"}, {"ded"}, {"ded"}, {"ded"}, {"ded"}}

Contest @@@ ProbSurvMale

{"live", "live", "live", "ded", "live", "ded", "ded", "ded", "ded", "live", "ded", "live", "ded", "live", "ded", "live", "ded", "live", "ded", "ded"}

$\endgroup$
4
  • $\begingroup$ thank you so much! the first input worked! is there a way i can message you privately? $\endgroup$
    – missalyssa
    Commented Jun 6, 2019 at 2:49
  • $\begingroup$ @missalyssa, thank you for the accept. You can post your questions here or open a chat room for extended discussions that will too long for the comments section. $\endgroup$
    – kglr
    Commented Jun 6, 2019 at 3:27
  • $\begingroup$ I posted another question if you still happen to be online. $\endgroup$
    – missalyssa
    Commented Jun 6, 2019 at 4:22
  • $\begingroup$ mathematica.stackexchange.com/questions/199835/… $\endgroup$
    – missalyssa
    Commented Jun 6, 2019 at 4:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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