Solving probability problems with MATLAB
40 views (last 30 days)
Paul Hoffrichter on 16 Dec 2020
You can simulate this problem using a Monte Carlo Simluation.
Here's a probablity MATLAB video:
How to Make Predictions Using Monte Carlo Simulations
Pat Gipper on 18 Dec 2020
Here is my take using a for loop. I wouldn't mind seeing how it is done without using a for loop
% Author: Pat Gipper
% Out of 100 apples, 10 are rotten. We randomly choose 5 apples without
% replacement. What is the probability that there is at least one rotten?
n=10000;% Start with 10,000 trials
x=[ones(1,10),zeros(1,90)];% 1st 10 apples are rotten, remaining 90 are OK
j=randperm(100);% Generate a random order of the indexes
y=x(j);% Randomly scatter the 10 rotten apples in the group of 100
z=sum(y(1:5))>0;% Is there a rotten apple in a group of 5?
p=p+z;% Increment the Monte-Carlo count for this trial if a bad apple turned up
p=p/n;% Calculate the probability from the n trials