Say I have a matrix of ones and twos. I want to randomly sample (without replacement) 2 rows with specified weights. This is what I have done here (I think?):
x = [1 1 1 2 1; 1 2 1 1 1; 1 1 1 1 1; 1 2 2 1 2; 1 2 2 1 1];
w1 = 1; % weight of ones
w2 = 4; % weight of twos
sum_wts = sum(w1.*(x==1),2) + sum(w2.*(x==2),2); %normalise weights
norm_weights = sum_wts./sum(sum_wts);
row_ind = randsample([1:size(x,1)], 2, true, norm_weights); %take random sample
new_x = x(row_ind,:);
I want the weight given to the twos to be additive. For example, a row containing 3 twos is more likely to be selected compared with a row containing 2 twos. Is this what I have done here? I'm unsure how to check if I have done this correctly...