如何在matlab中显示randperm()是公平的
假设,我想(从经验上)证明,来自matlab的randperm(n,k)确实从一组n个元素中产生大小为k的均匀分布随机样本。重复绘制后,如何绘制出现次数除以从N中绘制的k-子集总数的曲线?您可以简单地使用从如何在matlab中显示randperm()是公平的,matlab,probability,combinatorics,Matlab,Probability,Combinatorics,假设,我想(从经验上)证明,来自matlab的randperm(n,k)确实从一组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
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);
您可以简单地使用从
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);