x1 = [-27.445,-26.960,-26.635,-26.431,-26.315];
x2 = [-25.673,-25.497,-25.449,-25.491,-25.593];
x3 = [-29.310,-28.201,-27.240,-26.399,-25.654];
x4 = [-28.761,-27.103,-26.290,-25.605,-25.025];
我必须随机生成100个向量,但根据x1具有40%权重的概率,x2具有30%,x3具有20%,x4具有10%。
有人可以帮我在matlab中做到这一点吗?提前谢谢。
答案 0 :(得分:0)
您就是这样做的:
N=100;
W0=[40,30,20,10]'; %column vec
x1 = [-27.445,-26.960,-26.635,-26.431,-26.315];
x2 = [-25.673,-25.497,-25.449,-25.491,-25.593];
x3 = [-29.310,-28.201,-27.240,-26.399,-25.654];
x4 = [-28.761,-27.103,-26.290,-25.605,-25.025];
X0=cat(1,x1,x2,x3,x4);
assert(size(W0,1)==size(X0,1)) %ensure same size of W and X
W=W0/sum(W0); %normalize w to be probability vector
S=cumsum(W); % prepare the trick. we going to hit with rand() in range with proability
Rands=rand(1,N);
IsInRange=Rands<S; %for old matlab use: IsInRange=bsxfun(@lt,Rands,S)
[~,I]=max(IsInRange); %find the index
Result=X0(I,:)