How to avoid repetition in for loop that is using randi

2 ビュー (過去 30 日間)
Joel Schelander
Joel Schelander 2021 年 4 月 13 日
コメント済み: Joel Schelander 2021 年 4 月 14 日
I have the following loop where I fill IDCELL with different combinations of ID numbers. However, if a combination of ID numbers already exists, I want the loop to move on and try again. VID1, VID2 are two 3x3 cells.
for o=1:numel(INCREASE2)
for oz=1:999
index=randi([1,numel(VID1)]);
ID1 = VID1{index};
index2 = randi([1, numel(VID2)]);
ID2 = VID2{index2};
%ID1 and ID2 cannot be the same number
if numel(intersect(ID1,ID2))|| %some statement here
continue
else
IDCELL{o}={ID1; ID2};
break
end
end
end

採用された回答

Alan Stevens
Alan Stevens 2021 年 4 月 13 日
Does randperm do what you want?
help randperm

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by