所以让我先说我没有Matlab的统计工具箱,所以我试图找到解决这个问题的方法。无论如何,我要做的是复制R sample
函数。例如,在R
> x = sample(1:5,20,replace=T,prob=c(.1,.1,.1,.1,.6))
> x
[1] 5 5 5 4 5 2 5 5 1 5 5 5 5 5 5 3 5 1 5 5
所以我用替换整数1,2,3,4,5取样。但此外,我正在以一定比例对每个整数进行采样,即,整数5应该在大约60%的时间内被采样。
所以我想找到一个解决方案的问题是如何在Matlab中实现这个目标?
答案 0 :(得分:4)
以下是如何使用替换执行加权采样(Matlab的randsample
不支持,btw);
function r = sample(pop,n,weights)
%# each weight creates a "bin" of defined size. If the value of a random number
%# falls into the bin, we pick the value
%# turn weights into a normed cumulative sum
csWeights = cumsum(weights(:))/sum(weights);
csWeights = [0;csWeights(1:end-1)];
%# for each value: pick a random number, check against weights
idx = sum(bsxfun(@ge,rand(1,n),csWeights),1);
r = pop(idx);
答案 1 :(得分:3)
使用randi
function r = sample(pop, n)
imax = length(pop);
index = randi(imax, n, 1);
r = pop(index);
end
在加权的情况下,这样的事情可以解决问题:
function r = sample(pop, n, prob)
cumprob = cumsum(prob);
r = zeros(1, n);
for i = 1:n
index = find(rand < cumprob, 1, 'last');
r(i) = pop(index);
end
end
答案 2 :(得分:1)
这是制作自己的sample
功能的一种方法:
function x = sample(v, n, p)
pc = cumsum(p) / sum(p);
r = rand(1,n);
x = zeros(1,n);
for i = length(pc):-1:1
x(r<pc(i)) = v(i);
end
它并不完全有效,但它可以满足您的需求。像这样称呼它:
v = [1 2 3 4 5];
p = [.1 .1 .1 .1 .6];
n = 20;
x = sample(v,n,p);