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

Walter Roberson
Walter Roberson le 9 Nov 2011

25 votes

YourVector(randperm(length(YourVector))

5 commentaires

Miriam
Miriam le 9 Nov 2011
great! thanks a lot!!!
Alec Nagel
Alec Nagel le 3 Déc 2018
One ) missing at the end, but thanks! :)
Thanks a lot :))
Sakib Mahmud
Sakib Mahmud le 11 Jan 2023
Modifié(e) : Sakib Mahmud le 11 Jan 2023
YourVector = YourVector(randperm(length(YourVector)));
PANKAJ VADHVANI
PANKAJ VADHVANI le 4 Avr 2023
Thanks a lot :)

Connectez-vous pour commenter.

Plus de réponses (2)

Tomas
Tomas le 5 Fév 2016
Modifié(e) : Tomas le 5 Fév 2016

6 votes

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

shuffle = @(v)v(randperm(numel(v)));
Peeyush Awasthi
Peeyush Awasthi le 9 Fév 2019
It's indeed an smart solution to use randperm for any vector ar an array.
Chetna Patel
Chetna Patel le 29 Mar 2021
Thanks for this solution.

Connectez-vous pour commenter.

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!

Translated by