How to divide a vector randomly in 3 groups?
16 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Javier Fuster
il 19 Feb 2021
Commentato: Javier Fuster
il 24 Feb 2021
Hello everyone,
I have the following problem: I have a column vector A = [1:1000]' and I would like to divide it in 3 groups randomly. (For example in A1=[1:200], A2=[1:450], A3=[1:350]) It doesn't matter if the groups are contiguous. That is, if the first group is made up of sample 1 to 200, the second group should be made up of sample 201 to 650, etc.
The only requirement is that each group must contain at least 10% to 80% of the data. That is, there cannot be a group with 2 samples and the rest with 499 and 499.
Thanks in advance,
J.F.
2 Commenti
Risposta accettata
James Tursa
il 24 Feb 2021
Modificato: James Tursa
il 24 Feb 2021
Based on my current understanding, maybe this rejection method might do what you want. Again, since there are only three groups the rejection percentage of about 23% might be tolerable to you.
A = your vector
n = numel(A);
n10 = floor(0.10*n);
while( true )
p = n10 + sort(randperm(n-2*n10+1,2)-1);
if( p(1) >= n10 && p(2)-p(1) >= n10 && n-p(2) >= n10 )
break;
end
end
G = {A(1:p(1)),A(p(1)+1:p(2)),A(p(2)+1:end)};
Più risposte (1)
James Tursa
il 20 Feb 2021
Maybe a simple loop:
n = numel(A);
n10 = floor(0.10*n)-1;
n80 = floor(0.80*n);
for k=1:3
k1 = randi(n-n10);
k2 = k1 + n10 + randi(min(n-n10-k1+1,n80-n10)) - 1;
G{k} = A(k1:k2);
end
6 Commenti
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!