How to randomly select variable from the range of numbers ?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi,
I have a variable
a=[1:1:100]
I would like to select from the variable a , 5 values randomnly every time. Eg 10 15 67 89 99.
Is there any way to do it in simple rather than using a for loop.
Appreciate the help
0 Kommentare
Akzeptierte Antwort
Azzi Abdelmalek
am 26 Okt. 2012
a=[1:1:100]
out=a(randi(100,1,5))
5 Kommentare
Azzi Abdelmalek
am 26 Okt. 2012
Bearbeitet: Azzi Abdelmalek
am 26 Okt. 2012
in case it can't be repeated
[~,idx]=sort(rand(1,100))
out=a(idx(1:5))
Weitere Antworten (2)
Andrei Bobrov
am 26 Okt. 2012
Bearbeitet: Andrei Bobrov
am 26 Okt. 2012
a = 1:100;
ii = randperm(numel(a));
out = a(ii(1:5));
or
[ii,ii] = sort(rand(1,numel(a)));
out = a(ii(1:5));
or
out = a(randperm(numel(a),5));
0 Kommentare
Wayne King
am 26 Okt. 2012
Bearbeitet: Wayne King
am 26 Okt. 2012
idx = randperm(length(a));
idx = idx(1:5);
a(idx)
or if you have the newest version of MATLAB
idx = randperm(length(a),5);
a(idx)
0 Kommentare
Siehe auch
Kategorien
Mehr zu Logical 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!