Hello,

I want to implement a function using Matlab, that can be used to perform sampling without replacement with unequal weights.

- Without replacement:

When sampling without replacement each data point in the original dataset can appear at most once in the sample. The sample is therefore no larger than the original dataset.

- With unequal weights:

When sampling with unequal weights the probability of an observation from the

original dataset appearing in the sample is proportional to the weight assigned to that observation.

where the samples' weights should be changed at each iteration.

I have found this function, in the link:

function I=randsample_noreplace(n,k,w)

   I = randsample(n, k, true, w);

   while 1

        [II, idx] = sort(I);

         Idup = [false, diff(II)==0];

        if ~any(Idup)

            break 

       else

           w(I) = 0;                  %% Don't replace samples

           Idup (idx) = Idup;   %% find duplicates in original list

           I = [I(~Idup), (randsample(n, sum(Idup), true, w))];

      end

 end

where a vector of probabilities will be generated using random() function.

for example:

n=3; p=0.5; M=20; N=1;

random('Binomial',n,p,[M,N])

Any suggestion would be appreciated.

https://stackoverflow.com/questions/8205443/weighted-sampling-without-replacement

Similar questions and discussions