makedist discrete uniform distribution

7 views (last 30 days)
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?

Accepted Answer

Jeff Miller
Jeff Miller on 29 Nov 2018
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

More Answers (2)

Image Analyst
Image Analyst on 30 Nov 2018
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).
grid on;
0000 Screenshot.png

Jonathan Sewell
Jonathan Sewell on 25 Nov 2019
Edited: Jonathan Sewell on 25 Nov 2019
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