How can I draw without replacement?

조회 수: 4 (최근 30일)
Mint
Mint 2022년 1월 22일
편집: Mint 2022년 1월 22일
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)
  댓글 수: 1
Mint
Mint 2022년 1월 22일
편집: Mint 2022년 1월 22일
Thanks, that solves my problem! :)

댓글을 달려면 로그인하십시오.

추가 답변 (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