Why is the Matches variable too large?

조회 수: 1 (최근 30일)
Kiran Sai
Kiran Sai 2023년 5월 2일
댓글: Kiran Sai 2023년 5월 4일
I was playing around with a code that produces a simulator for the birthday paradox. Here's the piece of code:
ready = false;
while ~ ready
% User inputs and defining variables
birthday_repeats = input('select the number of birthday repeats from 10-500:');
if birthday_repeats > 500 || birthday_repeats < 10 || isempty(birthday_repeats) || round(birthday_repeats) ~= birthday_repeats|| isnan(birthday_repeats)
disp('error')
continue
end
sample_size = input('Select the sample size from 2-365: ');
if sample_size > 365 || sample_size < 2|| isempty(sample_size) || round(sample_size) ~= sample_size || isnan(sample_size)
disp('error')
continue
end
matches = zeros(1,sample_size);
N = 1000;
days = randi(1,365);
% simulation run
for k = 1:birthday_repeats % desired number of trials
matches = 0;
for j = 1:days
if test(days)
matches(sample_size) = matches(sample_size) + 1;
end
end
match_tally = matches(sample_size)/N;
end
I'm not sure why after each iteration of the loop the variable 'matches(sample_size)' is too large and here's the the 'test' function:
function out = test(data)
out = false;
n = length(data);
for k = 1:n
for i = k+1:n
if data(k) == data(i)
out = true;
break
end
end
end
end

채택된 답변

chicken vector
chicken vector 2023년 5월 2일
편집: chicken vector 2023년 5월 2일
Because you initialise the variable matches two times
You first set it as a vector, and then for each loop iteration you set it as a scalar.
matches = zeros(1,sample_size);
N = 1000;
days = randi(1,365);
for k = 1:birthday_repeats
matches = 0; %-------------------- DELETE THIS LINE --------------------%
for j = 1:days
if test(days)
matches(sample_size) = matches(sample_size) + 1;
end
end
match_tally = matches(sample_size)/N;
end
  댓글 수: 5
chicken vector
chicken vector 2023년 5월 3일
편집: chicken vector 2023년 5월 3일
You are doing a lot of unecessary for loops where each iteration just overwrites the results of the previous.
For this reason, it is not quite clear what you want to plot in th end.
Is this what you are looking for:
%% Input:
% disp('Welcome to the Birtday Paradox')
% birthday_repeats = input('select the number of birthday repeats from 10-500:');
% if birthday_repeats > 500 || birthday_repeats < 10 || isempty(birthday_repeats) || round(birthday_repeats) ~= birthday_repeats|| isnan(birthday_repeats)
% error('Wrong input');
% end
% sample_size = input('Select the sample size from 2-365: ');
% if sample_size > 365 || sample_size < 2|| isempty(sample_size) || round(sample_size) ~= sample_size || isnan(sample_size)
% error('Wrong input');
% end
% Uncomment previous and comment this:
birthday_repeats = 500;
sample_size = 23;
%% Process:
domain = 2 : sample_size;
match_tally = zeros(1,sample_size-1);
for j = 2 : sample_size
matches = zeros(1,j);
for k = 1 : birthday_repeats
birthdays = randi(365,1,j);
matches(k) = any(histcounts(birthdays,1:365) > 1);
end
match_tally(j-1) = sum(matches)/birthday_repeats;
end
expected_probability = 1 - prod(((365-sample_size+1):365)./365);
%% Plot:
figure;
hold on;
plot(domain,match_tally)
yline(expected_probability)
text(2,.97*expected_probability,['Expected probability for ' num2str(sample_size) ' people: ' num2str(expected_probability)])
grid on;
xlabel('Number of people chosen')
ylabel('Probability')
title('Birthday Paradox simulation')
subtitle([num2str(birthday_repeats) ' evaluations for each sample'])
Kiran Sai
Kiran Sai 2023년 5월 4일
Yeah, I just didn't understand how get my loop working as well as the formula to find the probability of matches. Thank you so much G.

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2023년 5월 2일
days = randi(1,365);
That does not request a random number between 1 and 365. That requests 365 x 365 random numbers in the range 1 to 1.
  댓글 수: 2
Kiran Sai
Kiran Sai 2023년 5월 2일
Will this:
days = randi(365,1,365)
days = 1×365
10 154 189 303 94 360 307 61 191 200 359 232 126 224 303 144 130 192 122 130 171 283 142 151 33 306 9 362 210 111
generate a 1x365 matrix containing random numbers
Walter Roberson
Walter Roberson 2023년 5월 2일
Yes, you can see from the summary output
days = 1x365
that it has generated a 1 x 365 array.
Note that many elements in days will be repeated. If you need to have a random permutation of the day numbers, use randperm

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

카테고리

Help CenterFile Exchange에서 Birthdays에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by