I have a population p
of indices and corresponding weights in vector w
. I want to get k
samples from this population without replacement where the selection is done proportional to the weights in random.
I know that randsample
can be used for selection with replacement by saying
J = randsample(p,k,true,w)
but when I call it with parameter false
instead of true
, I get
??? Error using ==> randsample at 184
Weighted sampling without replacement is not supported.
I wrote my own function as discussed in here:
p = 1:n;
J = zeros(1,k);
for i = 1:k
J(i) = randsample(p,1,true,w);
w(p == J(i)) = 0;
end
But since it has k
iterations in the loop, I seek for a shorter/faster way to do this. Do you have any suggestions?
EDIT: I want to randomly select k
unique columns of a matrix proportional to some weighting criteria. That is why I use sampling without replacement.