I'm asked to find the probability of this problem :(
1 view (last 30 days)
Show older comments
hello all, we had this problem in the class and we asked to verify the answer using MATLAB (answer=0.13867), so I write the following code:
if true
>> a= rand;
b=rand;
c=rand;
e=rand;
d=rand;
x=0.251;
n=0;
p=0;
for i=1: 10000
if (a<x)
a=1;
else
a=0;
end
if (b<x)
b=1;
else
b=0;
end
if (c<x)
c=1;
else
c=0;
end
if (d<x)
d=1;
else
d=0;
end
if (e<x)
e=1;
else
e=0;
end
p = (a*b)+ (a*e*d)+(c*d)+(c*e*b);
if (p>=1)
n=n+1;
end
end;
n/10000 end
and the answer appears is either 1 or 0.5, what is wrong? thanks ahead, I've to submitting it tomorrow, pls help
0 Comments
Accepted Answer
Kelly Kearney
on 5 Nov 2014
A few things...
- You're not changing anything within your loop. You probably want to be changing the values of a,b,c,d, and e on each iteration.
- You can replace each if statement with a = double(a < x).
- You can elimate the whole loop and do all the comparisons at once:
npt = 10000;
x = 0.251;
abcde = rand(npt,5);
isless = num2cell(double(abcde < x), 1);
[a,b,c,d,e] = deal(isless{:});
p = a.*b + a.*e.*d + c.*d + c.*e.*b;
frac = sum(p >= 1)/npt;
0 Comments
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!