Say I want to randomly select 3 rows from matrix m
m = [1 2 1; 1 1 1; 1 1 1; 2 1 1; 2 2 1; 1 1 1; 1 1 1; 2 1 1];
sample = m(randsample(1:length(m),3),:)
However, I want to randomly select rows with weights. So I need a vector that corresponds to each row of m
. I want to apply a weighting to each row depending on the number (or ratio) of 1
and 2
that are present in each row.
Perhaps something like this?
w1 = 0.6; %weight of 1
w2 = 0.4; %weight of 2
w = m;
w(w == 1) = w1;
w(w == 2) = w2;
w = (sum(w,2))*0.1 %vector of weights
But this is wrong:
sample = m(randsample(1:length(m),2),:,true,w)
Please help.
m(randsample(1:length(m),2,true,w),:)
. - Phil Goddard