Hi,all,
Does anybody know how to do random sample without replacement? The randsample function in matlab only supports sampling with replacement.
I made codes on my own, and it is really weird sometimes it works, but sometimes it shows error (Error using ==> randsample at 94 W must have length equal to N.):
function C=randsample_WithoutReplacement(m,n,A1,A2)%A1:population
%A2:probability
B=zeros(m,1);C=zeros(n,m);s=transpose(1:1:length(A1));ut=0;loc=0;A=A2;for j=1:n A=A2; s=transpose(1:1:length(A1)); for i=1:m B(i)=randsample(s,1,true,A); [ut, loc] = ismember(B(i), s); s(loc)=[]; A(loc)=[]; end for i=1:m C(j,i)=A1(B(i)); end end
Best Answer