MATLAB Answers

Miriam
0

shuffle numbers in a vector

Asked by Miriam
on 9 Nov 2011
Latest activity Commented on by Peeyush Awasthi on 9 Feb 2019
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!

  0 Comments

Sign in to comment.

3 Answers

Answer by Walter Roberson
on 9 Nov 2011
 Accepted Answer

YourVector(randperm(length(YourVector))

  2 Comments

great! thanks a lot!!!
One ) missing at the end, but thanks! :)

Sign in to comment.


Answer by Tomas
on 5 Feb 2016
Edited by 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

  2 Comments

shuffle = @(v)v(randperm(numel(v)));
It's indeed an smart solution to use randperm for any vector ar an array.

Sign in to comment.


Answer by Jan
on 5 Feb 2016

  0 Comments

Sign in to comment.