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 日

25 投票

YourVector(randperm(length(YourVector))

5 件のコメント

Miriam
Miriam 2011 年 11 月 9 日
great! thanks a lot!!!
Alec Nagel
Alec Nagel 2018 年 12 月 3 日
One ) missing at the end, but thanks! :)
SREENIVASULU REDDY KAMALAPURAM
SREENIVASULU REDDY KAMALAPURAM 2021 年 11 月 19 日
Thanks a lot :))
Sakib Mahmud
Sakib Mahmud 2023 年 1 月 11 日
編集済み: Sakib Mahmud 2023 年 1 月 11 日
YourVector = YourVector(randperm(length(YourVector)));
PANKAJ VADHVANI
PANKAJ VADHVANI 2023 年 4 月 4 日
Thanks a lot :)

サインインしてコメントする。

その他の回答 (2 件)

Tomas
Tomas 2016 年 2 月 5 日
編集済み: Tomas 2016 年 2 月 5 日

6 投票

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 件のコメント

Stephen23
Stephen23 2016 年 2 月 5 日
shuffle = @(v)v(randperm(numel(v)));
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.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File Exchange で Creating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by