shuffle numbers in a vector

701 次查看(过去 30 天)
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!

采纳的回答

Walter Roberson
Walter Roberson 2011-11-9
YourVector(randperm(length(YourVector))
  5 个评论
Sakib Mahmud
Sakib Mahmud 2023-1-11
编辑:Sakib Mahmud 2023-1-11
YourVector = YourVector(randperm(length(YourVector)));

请先登录,再进行评论。

更多回答(2 个)

Tomas
Tomas 2016-2-5
编辑:Tomas 2016-2-5
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 个评论
Peeyush Awasthi
Peeyush Awasthi 2019-2-9
It's indeed an smart solution to use randperm for any vector ar an array.
Chetna Patel
Chetna Patel 2021-3-29
Thanks for this solution.

请先登录,再进行评论。


Jan
Jan 2016-2-5

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by