shuffle numbers in a vector

1 903 views (last 30 days)
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!

Accepted Answer

Walter Roberson
Walter Roberson on 9 Nov 2011
YourVector(randperm(length(YourVector))
  3 Comments
SREENIVASULU REDDY KAMALAPURAM
SREENIVASULU REDDY KAMALAPURAM on 19 Nov 2021 at 11:17
Thanks a lot :))

Sign in to comment.

More Answers (2)

Tomas
Tomas on 5 Feb 2016
Edited: Tomas on 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 Comments
Chetna Patel
Chetna Patel on 29 Mar 2021
Thanks for this solution.

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!

Translated by