How can I draw without replacement?

3 次查看(过去 30 天)
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!

采纳的回答

Matt J
Matt J 2022-1-22
编辑:Matt J 2022-1-22
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)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by