How do you choose values from a distribution according to their respective probabilities?

6 次查看(过去 30 天)
I wrote the following codes which represent a distribution:
k=100;
i=1:k;
%p distribution (ISD)
p(i==1)=1/k;
p(i~=1)=1./(i(i~=1).*(i(i~=1)-1));
%T distribution
T(1<=i&i<=(k/R)-1)= R./(i(1<=i&i<=(k/R)-1).*k);
T(i==round(k/R))=((R*log(R/delta))/k);
T(i>=(k/R)+1&i<=k)=0;
%beta is normalization factor. %It is calculated by adding both 1x100 arrays of p and T and summing each %array.
beta=sum(p+T);
%RSD
M=(p+T)/beta;
stem(i,M);
Now, I want to choose i from M. How can I do this?
Any help would be much appreciated. Thanks

回答(1 个)

Tom Lane
Tom Lane 2012-11-16
Consider the randsample function if you have the Statistics Toolbox, or look here:

Community Treasure Hunt

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

Start Hunting!

Translated by