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 CenterFile Exchange에서 Random Number Generation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by