Why is the Matches variable too large?
1 回表示 (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
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
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'])
その他の回答 (1 件)
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 件のコメント
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
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!