# Solving probability problems with MATLAB

조회 수: 30(최근 30일)
Gabriel Agbese 2020년 12월 16일
답변: Pat Gipper 2020년 12월 18일
How can I simulate this question using MATLAB?
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?

댓글을 달려면 로그인하십시오.

### 답변(2개)

Paul Hoffrichter 2020년 12월 16일
You can simulate this problem using a Monte Carlo Simluation.
Here's a probablity MATLAB video:
How to Make Predictions Using Monte Carlo Simulations
##### 댓글 수: 0표시숨기기 이전 댓글 수: -1

댓글을 달려면 로그인하십시오.

Pat Gipper 2020년 12월 18일
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?
x=[ones(1,10),zeros(1,90)];% 1st 10 apples are rotten, remaining 90 are OK
p=0;% Accumulator
%
for i=1:n
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
end
p=p/n;% Calculate the probability from the n trials

댓글을 달려면 로그인하십시오.

R2020b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by