Random sample without replacement
% Random smapling with weight and without replacement
% O/P : sample--> random sample
% I/P : N--> population; n--> sample size; W--> weightage vector
function sample = RandSampleWR(N,n,W)
%% checking
if(length(N)<=0)||(n<=0)||(length(N)<=n)
disp('Populationsize and sample size must be greater than zero and sample size must be less than population size.')
close
end
if (length(N) ~= length(W))
disp('Population size and weightage vector size must be same.')
close
end
%% initialization
maxN = length(N);
k = 1;
W = W/sum(W(1:maxN));
%% Knuth-Fisher-Yates sampling
for i=n:-1:1
r1 = ceil(rand*maxN);
r2 = rand;
while(r2>W(r1))
r1 = ceil(rand*maxN);
r2 = rand;
end
sample(k) = N(r1);
temp = N(maxN);
temp1 = W(r1);
temp2 = W(maxN);
N(maxN) = sample(k);
N(r1) = temp;
W(maxN) = temp1;
W(r1) = temp2;
maxN = maxN-1;
W = W/sum(W(1:maxN));
k = k+1;
clear temp temp1 temp2 r1 r2
end
Cite As
Kaushik Bhattacharjee (2024). Random sample without replacement (https://www.mathworks.com/matlabcentral/fileexchange/44261-random-sample-without-replacement), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Platform Compatibility
Windows macOS LinuxCategories
- MATLAB > Mathematics > Sparse Matrices >
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.
Version | Published | Release Notes | |
---|---|---|---|
1.1.0.0 | correction of a small mistake |