从混合物中生成随机数,并按顺序放置每个分布的结果

时间:2015-12-23 18:04:23

标签: r random statistics

我想将随机数放在向量中的第一个gamma A = randi(255,737280,1); B = randi(255,737280,1); C = zeros(size(A)); b_vals = unique(B); for l = 1:numel(b_vals) b_inds = find(B==b_vals(l)); %// find the indices of each unique value in B a_inds = find(A==b_vals(l)); %// find the indices of each unique value in A %// in case the length of a_inds is greater than the length of b_inds %// duplicate b_inds until it is larger (or equal) b_inds = repmat(b_inds,[ceil(numel(a_inds)/numel(b_inds)),1]); %// truncate b_inds to be the same length as a_inds (if necessary) and %// put b_inds into the proper places in C C(a_inds) = b_inds(1:numel(a_inds)); end 从另一个向量中的第二个伽玛[gamma with shape=2 , rate=4]

[gamma with shape=3 , rate=4]

1 个答案:

答案 0 :(得分:0)

你可以尝试(如果我明白你的意思):

rgamma(20,shape=sample(2:3,20,replace=TRUE,prob=c(0.8,0.2)),rate=4)

shape sample来自2:3,而{2}概率为80%。然后你可以只调用rgamma一次,因为它的矢量化性质。

如果您需要两个不同的向量,您可以:

nShape2<-rbinom(1,20,.80)
vecShape2<-rgamma(nShape2,shape=2,rate=4)
vecShape3<-rgamma(20-nShape2,shape=3,rate=4)