Question about using randperm in a loop
Ältere Kommentare anzeigen
Hi,
I'm using randperm in a loop that's meant to generate unique random three-digit numbers to fill a 5x5 matrix, r, like this:
for i = 1:length(r)
r(1:5,i) = (randperm(900,5) + 99);
end
My question is:
Is there a possibility of repeating a number given the function is being called 5 times, once for each column? I want each number to be unique.
Thanks,
Veena
Akzeptierte Antwort
Weitere Antworten (1)
James Tursa
am 10 Mär. 2020
Bearbeitet: James Tursa
am 10 Mär. 2020
If you want each number in the matrix to be unique, then don't call randperm( ) by row because that will not guarantee uniqueness between calls. Call randperm( ) only once for the entire matrix (25 elements) and then reshape to a 5x5. E.g., something like
r = reshape(randperm(900,25) + 99,5,5);
1 Kommentar
Veena Chatti
am 10 Mär. 2020
Kategorien
Mehr zu Data Type Conversion finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!