假设,我想(凭经验(证明matlab中的randperm(n,k(确实从n个元素的集合n中产生了大小为k的均匀分布随机样本。重复绘制后,如何绘制出现次数除以从N绘制的k个子集总数?
您可以简单地使用从randperm
绘制的索引来递增计数器向量。
n=1e5;
k=1e4;
maxiter = 1e5;
% This array will be used to count the number of times each integer has been drawn
count=zeros(n,1);
for ii=1:maxiter
p=randperm(n,k);
% p is a vector of k distinct integers in the 1:n range
% the array count will be incremented at indices given by p
count(p)=count(p)+1;
end
% A total of k*maxiter integers has been drawn and they should be evenly
% distributed over n values
% The following vector should have values close to 1 for large values of maxiter
prob = count*n/(k*maxiter);