Plotting a graph on MATLAB

2 vues (au cours des 30 derniers jours)
Sam Loie
Sam Loie le 11 Jan 2021
I am new to matlab and i am trying to plot a graph to do with the birthday paradox. The problem i am having is that when i try to plot the graph of number of people against probability i get one dot in the middle. Even when moving the plot function inside the for loop the plot is still wrong.
this my output but it goes to 100%:
this is my current plot which is wrong:
here's my code:
% Write your code here:
%Inputs
tic;
nPeopleMax=200;
R=50;
%Randomizing
count=0; %counts the number of times atleast one birthday matches
for k=1:R
M=randi(365,nPeopleMax,1);
Sets=nchoosek(M,2);
for i=1:size(Sets,1)
if(Sets(i,1)==Sets(i,2))
count=count+1;
Probability=(count/R)*100;
plot(nPeopleMax,Probability, '.');
disp(['The Probability is ',num2str(Probability), ' %']);
break
end
end
end
toc;

Réponses (1)

Doddy Kastanya
Doddy Kastanya le 11 Jan 2021
In order to see more "dots", you need to add "hold on" on the line after plot. However, since you want to plot it against the number of people, you might want to use "nPeopleMax" in the plot, since the value will always be 200. Good luck.

Catégories

En savoir plus sur Birthdays dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by