shuffle numbers in a vector
Afficher commentaires plus anciens
Hi, I have a vector which has n by 1 entries. I want to randomly shuffle the numbers in this vector! Thanks in advance for any sugestions regarding that!
Réponse acceptée
Plus de réponses (2)
To shuffle vectors without saving them to a variable first, e.g. to shuffle a for-loop, I recommend adding a function like this to your repertoire:
function v=shuffle(v)
v=v(randperm(length(v)));
end
e.g.
for i=shuffle(1:10)
disp(i)
end
3 commentaires
Stephen23
le 5 Fév 2016
shuffle = @(v)v(randperm(numel(v)));
Peeyush Awasthi
le 9 Fév 2019
It's indeed an smart solution to use randperm for any vector ar an array.
Chetna Patel
le 29 Mar 2021
Thanks for this solution.
Jan
le 5 Fév 2016
1 vote
Catégories
En savoir plus sur Loops and Conditional Statements 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!