Random generator with constraints

1 vue (au cours des 30 derniers jours)
Kathryn Holder
Kathryn Holder le 26 Fév 2021
Commenté : Adam Danz le 26 Fév 2021
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.

Réponse acceptée

Adam Danz
Adam Danz le 26 Fév 2021
> 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 commentaires
Kathryn Holder
Kathryn Holder le 26 Fév 2021
Thanks!
Adam Danz
Adam Danz le 26 Fév 2021
Steven Lord's approach is simpler for the first part of the question.

Connectez-vous pour commenter.

Plus de réponses (1)

Steven Lord
Steven Lord le 26 Fév 2021
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

Catégories

En savoir plus sur Random Number Generation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by