makedist discrete uniform distribution

12 ビュー (過去 30 日間)
Nikolas Spiliopoulos
Nikolas Spiliopoulos 2018 年 11 月 29 日
編集済み: Jonathan Sewell 2019 年 11 月 25 日
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

採用された回答

Jeff Miller
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

その他の回答 (2 件)

Image Analyst
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');
0000 Screenshot.png

Jonathan Sewell
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.

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by