Given a vector, I would like to sample without replacement elements from it repeatedly.

1 次查看(过去 30 天)
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 个评论
Matt J
Matt J 2020-8-21
编辑:Matt J 2020-8-21
However, I want to do this repeatedly without using a for loop
If you make s>1, you will receive more than one selected sample. It will give you s samples in a single call.

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2020-8-21
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 个评论

请先登录,再进行评论。

更多回答(1 个)

Bruno Luong
Bruno Luong 2020-8-21
编辑:Bruno Luong 2020-8-21
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)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by