Given a vector, I would like to sample without replacement elements from it repeatedly.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Cem Gormezano
am 21 Aug. 2020
Kommentiert: Matt J
am 21 Aug. 2020
Given a (m x 1) vector v , I would like to ,randomly without replacement, sample s elements from it. I know I can use randsample(v,s) if I were to do this once. However, I want to do this repeatedly without using a for loop (i.e vectorization) so that it is fast.
1 Kommentar
Akzeptierte Antwort
Matt J
am 21 Aug. 2020
m=10; n=15; s=3;
V=rand(m,n); %hypothetical data
[~,ids] = sort( rand(size(V)) ,1);
ids=ids(1:s,:)+m*(0:n-1);
selection=V(ids) %selects s elements from each column of V
4 Kommentare
Matt J
am 21 Aug. 2020
You're quite welcome, but please Accept-click the answer if it resolved your issue.
Weitere Antworten (1)
Bruno Luong
am 21 Aug. 2020
Bearbeitet: Bruno Luong
am 21 Aug. 2020
v = 'a':'z' % your vector
n = 10; % number of "loop"
s = 3; % number of drawing without replacement
[~,ir] = maxk(rand(n,length(v)),s,2);
r = v(ir)
0 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements 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!