Random generator with constraints

1 回表示 (過去 30 日間)
Kathryn Holder
Kathryn Holder 2021 年 2 月 26 日
コメント済み: Adam Danz 2021 年 2 月 26 日
I am getting back into MATLAB and need to create a random generator that provides a matrix of 12 rows by 4 colums of numbers from (1,4), with non repeating numbers in row. And another separate random generator code that can provide a 1 row by 5 colums of numbers for (0,4), with non repeating numbers in row.

採用された回答

Adam Danz
Adam Danz 2021 年 2 月 26 日
> I am getting back into MATLAB and need to create a random generator that provides a matrix of 12 rows by 4 colums of numbers from (1,4), with non repeating numbers in row
sz = [12,4];
maxVal = 4;
M = cell2mat(arrayfun(@(i){randperm(maxVal,sz(2))},1:sz(1))');
>And another separate random generator code that can provide a 1 row by 5 colums of numbers for (0,4), with non repeating numbers in row.
V = randperm(5,5).'-1;
  2 件のコメント
Kathryn Holder
Kathryn Holder 2021 年 2 月 26 日
Thanks!
Adam Danz
Adam Danz 2021 年 2 月 26 日
Steven Lord's approach is simpler for the first part of the question.

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

その他の回答 (1 件)

Steven Lord
Steven Lord 2021 年 2 月 26 日
A = rand(6, 4);
[~, ind] = sort(A, 2)
ind = 6×4
4 1 2 3 4 2 3 1 1 2 3 4 4 2 1 3 2 4 3 1 1 3 2 4

カテゴリ

Help Center および File ExchangeRandom Number Generation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by