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!

 Respuesta aceptada

Walter Roberson
Walter Roberson el 9 de Nov. de 2011

25 votos

YourVector(randperm(length(YourVector))

5 comentarios

Miriam
Miriam el 9 de Nov. de 2011
great! thanks a lot!!!
Alec Nagel
Alec Nagel el 3 de Dic. de 2018
One ) missing at the end, but thanks! :)
SREENIVASULU REDDY KAMALAPURAM
SREENIVASULU REDDY KAMALAPURAM el 19 de Nov. de 2021
Thanks a lot :))
Sakib Mahmud
Sakib Mahmud el 11 de En. de 2023
Editada: Sakib Mahmud el 11 de En. de 2023
YourVector = YourVector(randperm(length(YourVector)));
PANKAJ VADHVANI
PANKAJ VADHVANI el 4 de Abr. de 2023
Thanks a lot :)

Iniciar sesión para comentar.

Más respuestas (2)

Tomas
Tomas el 5 de Feb. de 2016
Editada: Tomas el 5 de Feb. de 2016

6 votos

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 comentarios

Stephen23
Stephen23 el 5 de Feb. de 2016
shuffle = @(v)v(randperm(numel(v)));
Peeyush Awasthi
Peeyush Awasthi el 9 de Feb. de 2019
It's indeed an smart solution to use randperm for any vector ar an array.
Chetna Patel
Chetna Patel el 29 de Mzo. de 2021
Thanks for this solution.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 9 de Nov. de 2011

Comentada:

el 4 de Abr. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by