How to randomise numbers in a vector?
24 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Bianca Elena Ivanof
am 17 Dez. 2016
Bearbeitet: Jan
am 19 Dez. 2016
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 Kommentare
Akzeptierte Antwort
Jan
am 17 Dez. 2016
Bearbeitet: Jan
am 17 Dez. 2016
5 Kommentare
Bianca Elena Ivanof
am 18 Dez. 2016
Bearbeitet: Bianca Elena Ivanof
am 18 Dez. 2016
Weitere Antworten (1)
Andrei Bobrov
am 17 Dez. 2016
Bearbeitet: Andrei Bobrov
am 17 Dez. 2016
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 Kommentare
Jan
am 18 Dez. 2016
Bearbeitet: Jan
am 18 Dez. 2016
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.
Siehe auch
Kategorien
Mehr zu Colormaps finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!