看板 MATLAB 關於我們 聯絡資訊
各位好我是自學MATLAB新手, 有個小問題想請教大家。 例如大樂透49取6的程式碼如下: x1 = round(rand*48)+1; x2 = round(rand*48)+1; x3 = round(rand*48)+1; x4 = round(rand*48)+1; x5 = round(rand*48)+1; x6 = round(rand*48)+1; x=[x1 x2 x3 x4 x5 x6]; if(x1~=x2)&(x1~=x3)&(x1~=x4)&(x1~=x5)&(x1~=x6)&(x2~=x3)&(x2~=x4)&(x2~=x5)&(x2~=x6)& (x3~=x4)&(x3~=x5)&(x3~=x6)&(x4~=x5)&(x4~=x6)&(x5~=x6) x end 假如6個亂數產生的號碼真的有重複, 那是否會直接跳出if而沒有東西產生呢? 該如何讓它重run至產生6個不重複數字為止? 抑或是MATLAB本來就會直接重新選亂數直到if式成立為止? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.113.0.109
CBET:請利用 while 迴圈、randi、length、和 unique 11/06 16:34
mp19990920:不用迴圈啦! 用 X=randperm(49); x = X(1:6);即可 11/06 23:59
CBET:我耍笨了... =口=" 11/07 00:23