Hi~
There is A=[1:1:37] in matlab, two numbers are randomly drawn from the list A simultaneously, and a total of 74 times are drawn. Requires are as fllows:
1. Each number is drawn 4 times in total.
2. And the same number will not appear twice every time you draw
3. The same number will not be drawn for two consecutive trials.
The final result is expressed as a 74×2 list, each row represents an extraction, the first column indicates the first number drawn, and the second column indicates the second number drawn.

6 件のコメント

Stephen23
Stephen23 2023 年 8 月 2 日
It looks like you will need a WHILE loop, or something similar. What have you tried so far?
Jane
Jane 2023 年 8 月 2 日
I have tried:
"drawnNumbers = datasample(fullList, 2, 'Replace', false);"
I wonder know how to fufill the reqirement 2?
Stephen23
Stephen23 2023 年 8 月 2 日
"I wonder know how to fufill the reqirement 2?"
By specifying "Replace"=false you have already fulfilled requirement 2.
Jane
Jane 2023 年 8 月 2 日
I misread it just now, it should be requirement three.
Stephen23
Stephen23 2023 年 8 月 2 日
編集済み: Stephen23 2023 年 8 月 2 日
"I misread it just now, it should be requirement three."
You could make a sample, then test if its elements are different compared to the last sample. If the values are duplicated make another sample (e.g. a WHILE loop).
However you should be thinking about requirement 1, because how you approach that will determine the entire structure of your algorithm and code, probably more than requirements 2 and 3.
Jane
Jane 2023 年 8 月 3 日
Yes, very sensible~

サインインしてコメントする。

 採用された回答

Bruno Luong
Bruno Luong 2023 年 8 月 2 日
編集済み: Bruno Luong 2023 年 8 月 2 日

0 投票

succeed = false;
while ~succeed
c = repelem(4,37,1);
n = sum(c);
r = rand(1,n);
y = [];
for i=1:n
cn = c;
cn(y) = 0;
cc = cumsum(cn);
s = cc(end);
succeed = s > 0;
if ~succeed
break
end
x = discretize(r(i), [0; cc/s]);
r(i) = x;
c(x) = c(x)-1;
y = r(max(i-mod(i,2)-1,1):i);
end
end
r = reshape(r,2,[])';
r
r = 74×2
15 22 9 13 22 2 11 35 1 26 25 20 19 4 22 25 17 13 23 22

2 件のコメント

Jane
Jane 2023 年 8 月 3 日
Fabulous! Thanks a lot.
Bruno Luong
Bruno Luong 2023 年 8 月 3 日
You are welcome.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

リリース

R2017b

質問済み:

2023 年 8 月 2 日

コメント済み:

2023 年 8 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by