How can I draw without replacement?

3 Ansichten (letzte 30 Tage)
Mint
Mint am 22 Jan. 2022
Bearbeitet: Mint am 22 Jan. 2022
I want a 30-by-40 matrix where each element is one of {1,2,3,4,5,6,7,8,9,10,0,0,0,0,0,0,0, 0,0,0} (or values 1 to 10 and the rest zeros). I have N=30, each going through 40 passes. First the first 20 values are to be drawn without replacement, and then the next 20 values without replacement, so that there are 40 draws per N. The values should then be saved in a text file, separated by commas. Thank yor for all suggestions!

Akzeptierte Antwort

Matt J
Matt J am 22 Jan. 2022
Bearbeitet: Matt J am 22 Jan. 2022
S=[1,2,3,4,5,6,7,8,9,10,0,0,0,0,0,0,0, 0,0,0];
[~,is1]=sort(rand(30,20),2);
[~,is2]=sort(rand(30,20),2);
result=S([is1,is2])
result = 30×40
3 9 0 0 4 10 5 0 8 0 6 0 0 0 0 0 1 0 2 7 10 0 0 6 9 0 0 4 1 0 0 0 3 0 0 4 8 0 9 1 7 2 0 5 10 0 6 0 0 0 0 10 5 0 2 0 3 0 9 4 0 6 7 0 9 0 5 1 0 8 0 3 0 0 4 0 2 0 0 10 0 0 0 0 9 7 3 0 10 8 0 0 6 0 4 10 0 9 0 5 0 7 2 8 0 0 3 0 1 0 5 9 8 3 0 4 0 0 10 1 1 0 2 0 8 9 7 0 0 6 0 4 10 0 0 0 3 5 0 0 8 0 0 5 9 6 3 2 0 0 0 0 0 8 9 7 0 6 0 0 0 0 2 3 4 0 5 10 0 1 0 0 4 0 0 1 0 0 5 10 0 0 7 1 10 9 0 3 4 8 6 0 0 5 0 0 2 0 0 0 10 1 0 3 0 0 0 0 0 0 0 10 0 0 0 0 0 4 0 0 0 6 8 9 1 5 0 3 7 2 4 1 0 0 0 0 2 0 0 9 9 0 0 0 0 0 8 7 0 2 4 0 1 0 5 6 0 3 10 0 0 0 4 0 0 0 7 0 3 0 7 0 5 0 0 9 0 0 8 0 2 6 0 0 0 10 1 0 3 4 0 0 8 10 0 7 6 0 0 2
writematrix(result)
  1 Kommentar
Mint
Mint am 22 Jan. 2022
Bearbeitet: Mint am 22 Jan. 2022
Thanks, that solves my problem! :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by