How can I select randomly?
Afficher commentaires plus anciens
Hello, I have an 10000 rows and 10 columns matrix. I want to select randomly 500 rows from this matrix. I want to ask you, randperm function is true for this purpose. How can I select 500 rows randomly from this matrix?
Réponse acceptée
Plus de réponses (1)
M = rand(10000, 10);
index = randperm(10000, 500); % In modern Matlab versions
R = M(index, :);
In older Matlab versions randperm does not accept a 2nd input. Then:
index = randperm(10000);
index = index(1:500);
index = Shuffle(10000, 'index', 500)
7 commentaires
Selin Soguksu
le 12 Déc 2012
Selin Soguksu
le 13 Déc 2012
Jan
le 13 Déc 2012
@Beril: Ouch. In my example M is only created to have any test data. In the real program this line should be omitted, of course. Your question contains only the description "I have an 10000 rows and 10 columns matrix" and I filled it with random data. This is a usual method in a forum, because it allows to test the code before posting it.
Image Analyst
le 14 Déc 2012
I could hear the sound of a hand slapping a forehead all the way across the Atlantic. ;-)
Matt Fig
le 14 Déc 2012
Apparently saying, "M is your original matrix" would have made all the difference ;-).
Selin Soguksu
le 19 Déc 2012
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!