shuffle numbers in a vector

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!

 Risposta accettata

Walter Roberson
Walter Roberson il 9 Nov 2011
YourVector(randperm(length(YourVector))

5 Commenti

Miriam
Miriam il 9 Nov 2011
great! thanks a lot!!!
Alec Nagel
Alec Nagel il 3 Dic 2018
One ) missing at the end, but thanks! :)
Thanks a lot :))
Sakib Mahmud
Sakib Mahmud il 11 Gen 2023
Modificato: Sakib Mahmud il 11 Gen 2023
YourVector = YourVector(randperm(length(YourVector)));
Thanks a lot :)

Accedi per commentare.

Più risposte (2)

Tomas
Tomas il 5 Feb 2016
Modificato: Tomas il 5 Feb 2016
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 Commenti

shuffle = @(v)v(randperm(numel(v)));
It's indeed an smart solution to use randperm for any vector ar an array.
Chetna Patel
Chetna Patel il 29 Mar 2021
Thanks for this solution.

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by