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

3 views (last 30 days)
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 Comment
John Bennett
John Bennett on 16 Nov 2012
Some additional information if you would want to run the code to see how the graph is like.
c=0.1; k=100; delta=0.1;
R=c*log(k/delta)*sqrt(k);

Sign in to comment.

Answers (1)

Tom Lane
Tom Lane on 16 Nov 2012
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!