Picking random points...
11 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Joe Bannen
am 5 Nov. 2015
Kommentiert: Thorsten
am 5 Nov. 2015
Hi
I am exploring a closed (time) interval between 0 and 1. I have used the linspace command to generate it:
t=linspace(0,1);
I can't seem to figure out how to randomly pick 10 (say) of these points in t so that I can plug them into a function (selected in a non-uniform way). Any thought?
Many thanks
Joe
0 Kommentare
Akzeptierte Antwort
Stephen23
am 5 Nov. 2015
Bearbeitet: Stephen23
am 5 Nov. 2015
You can simply generate some indices to pick value from the vector t. I have shown you uniformly distributed random indices, because you did not tell us what random distribution you want. Two solutions are shown below: without and with index repetition.
t = linspace(0,1);
N = 5; % how many points to pick
>> idx = randperm(numel(t),N)
idx =
45 75 58 51 15
>> t(idx)
ans =
0.44444 0.74747 0.57576 0.50505 0.14141
>> idy = randi(numel(t),1,N)
idy =
53 44 11 77 71
>> t(idy)
ans =
0.52525 0.43434 0.10101 0.76768 0.70707
4 Kommentare
Thorsten
am 5 Nov. 2015
Maybe you want to sort the 10 randomly picked numbers:
t=linspace(0,1);
t10 = t(sort(randperm(numel(t), 10)))
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!