Borrar filtros
Borrar filtros

Plotting a graph on MATLAB

1 visualización (últimos 30 días)
Sam Loie
Sam Loie el 11 de En. de 2021
Respondida: Doddy Kastanya el 11 de En. de 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;

Respuestas (1)

Doddy Kastanya
Doddy Kastanya el 11 de En. de 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.

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by