makedist discrete uniform distribution
조회 수: 7 (최근 30일)
이전 댓글 표시
Hi all
I am trying to create a uniform discrete distribution, with 5 values (20% probability each), by using makedist command
However, I am bit confused with the boundaries.
My values I want to be : 20,25,30,35,40
Can you please help?
thanks
Nikolas
댓글 수: 0
채택된 답변
Jeff Miller
2018년 11월 29일
It appears that MATLAB will only give you random integers 1..K for any K you want, so you will have to convert those to your desired values, e.g.:
myrandoms = 15 + randi(5,100,1)*5 % A vector of 100 random numbers from your set
댓글 수: 0
추가 답변 (2개)
Image Analyst
2018년 11월 30일
This probably isn't what you want, but the code below will give you those 5 numbers, occurring at random locations in an array. And the probability will definitely be exactly 20% in each bin. Exactly 20% - no variation in the count because we're not using randi(). However the values are not random - they are exactly what you said - you are not going to get a count of 832 for 25, 803 for 30, 799 for 35, etc.. You will get the same count for all of them. You can specify approximately 20% in each bin (this is probably what you want, in which case you're use randi like Jeff did) OR you can specify that you want EXACTLY 20% to be each number (what I did).
% Define the values.
specifiedValues = [20,25,30,35,40]
% Make copies
numCopies = 300; % However many values you want to generate.
distributionValues = repmat(specifiedValues, [1, numCopies]);
% OPTIONAL: Scramble up (randomize) the order.
sortingOrder = randperm(length(distributionValues));
distributionValues = distributionValues(sortingOrder);
% Prove that the distribution is exactly 100% uniform (same count in each bin).
histogram(distributionValues);
grid on;
xticks(specifiedValues);
xlabel('Value');
ylabel('Count');
댓글 수: 0
Jonathan Sewell
2019년 11월 25일
편집: Jonathan Sewell
2019년 11월 25일
A function that achieves the random sampling effect you want is randsample().
To get one random sample from your specified population, use this.
randsample([20 25 30 35 40], 1)
To get ten random samples, use this.
randsample([20 25 30 35 40], 10, 1)
I do not know how to get this effect out of a call to makedist(). However, you can create your own distribution object that behaves like objects returned by makedist(). This would involve creating a class. It will need to be a subclass of ProbabilityDistribution, and probably a subclass of UnivariateDistribution or TruncatableDistribution as well. These classes are part of the shared stats library that makedist uses.
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!