Plotting a graph on MATLAB

2 次查看(过去 30 天)
Sam Loie
Sam Loie 2021-1-11
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;

回答(1 个)

Doddy Kastanya
Doddy Kastanya 2021-1-11
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.

类别

Help CenterFile Exchange 中查找有关 Birthdays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by