# Solving probability problems with MATLAB

2 visualizaciones (últimos 30 días)
Gabriel Agbese el 16 de Dic. de 2020
Comentada: Estela el 15 de Dic. de 2022
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?
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Respuestas (3)

Walter Roberson el 15 de Dic. de 2022
SampleSize = 100;
NumTrials = 10000;
[~, randomized] = sort(rand(SampleSize, NumTrials), 1);
num_bad_in_first_five = sum(randomized(1:5,:) <= 10, 1);
counts = accumarray(num_bad_in_first_five.' + 1, 1, [6 1]);
bar(0:5, counts ./ NumTrials * 100); title('% of samples with exactly this many bad apples')
##### 2 comentariosMostrar NingunoOcultar Ninguno
Estela el 15 de Dic. de 2022
THANK YOU! Any way to use poisscdf/binocdf/normcdf somewhere in there?
Estela el 15 de Dic. de 2022

Iniciar sesión para comentar.

Paul Hoffrichter el 16 de Dic. de 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
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Pat Gipper el 18 de Dic. de 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?
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
##### 3 comentariosMostrar 1 comentario más antiguoOcultar 1 comentario más antiguo
Walter Roberson el 15 de Dic. de 2022
% 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
whos p
Name Size Bytes Class Attributes p 1x1 8 double
You can see that the result is a scalar. There is nothing useful to plot about a scalar.
Estela el 15 de Dic. de 2022
Editada: Estela el 15 de Dic. de 2022
Any idea of a problem using Probability functions such as poisscdf,binocdf, normcdf example that I can plot? I am currentl ytrying to figure one out I would really appreciate the help!

Iniciar sesión para comentar.

### Categorías

Más información sobre Server Management en Help Center y File Exchange.

R2020b

### Community Treasure Hunt

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

Start Hunting!

Translated by