How to randomise numbers in a vector?
24 ビュー (過去 30 日間)
古いコメントを表示
Dear all,
Suppose I have this vector x= [1;2;3;4];
How can I randomise it? (i.e. create different combinations of 1, 2, 3 and 4)
Thank you very much in advance, Bianca
0 件のコメント
採用された回答
その他の回答 (1 件)
Andrei Bobrov
2016 年 12 月 17 日
編集済み: Andrei Bobrov
2016 年 12 月 17 日
Hi Elena!
One way:
x= [1 1; 1 2; 1 3; 1 4]
[~,ii] = sort(rand(size(x,1),1));
out = x(ii,:);
or just
out = x(randperm(size(x,1)),:);
2 件のコメント
Jan
2016 年 12 月 18 日
編集済み: Jan
2016 年 12 月 18 日
randperm uses the Fisher-Yates shuffle now (as FEX: Shuffle), which is more accurate than SORT(RAND). The later is a stable sort, so if two elements replied by RAND are equal (unlikely, but not impossible) the sorting order is not random. If you e.g. have to shuffle a vector of 2^52 elements, uuhm, well... Who cares.
参考
カテゴリ
Help Center および File Exchange で Colormaps についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!