Randperm in for loop

8 ビュー (過去 30 日間)
Marien Ochoa
Marien Ochoa 2020 年 11 月 7 日
コメント済み: Ameer Hamza 2020 年 11 月 7 日
I am using RANDPERM to select 1 unique number from the following matrix [2 3 4] per each cycle of a For loop. I want RANDPERM to give a different number each for loop cycle, but first it gives 2, 3 at least one time but then is just giving 4 for the rest of for loop cycles. I already tried using "RGN SHUFFLE" but it doesn't work as it still does the same thing. Any suggestion?
Thank you!

回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 11 月 7 日
I am not sure how you are using randperm, but it should not be needed in this case. Something like the following will work
x = [2 3 4];
for i = 1:10
idx = randi(numel(x));
rand_val = x(idx)
end
  2 件のコメント
Marien Ochoa
Marien Ochoa 2020 年 11 月 7 日
Thanks so much! Yes this is better.
Ameer Hamza
Ameer Hamza 2020 年 11 月 7 日
I am glad to be of help!!!

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by