Applying probability concept using matlab

2 views (last 30 days)
Hello Experts,
I have a question that has a little something to do with probability that I'd like to work on. So I'd like to understand it on a small scale so I can apply it to a bigger one.
I have a group with four numbers, and I need to select a number from this group, I used randperm(4) so the number will be a random permutation from 1 to 4,
How would you make such that there is 75% possibility for number 2 to pop out as when you choose a number from this random permutation? thank you!

Answers (4)

the cyclist
the cyclist on 17 Jun 2012
I am not sure I understand your question, but I think you can do what you want using the randsample() function. (For example, you can select different elements with different probabilities by adjusting the weights).
  1 Comment
NUR KHAIRUNNISA rahimi
NUR KHAIRUNNISA rahimi on 17 Jun 2012
let say i have a group of 4 people, and each of these 4 people is represented by the number 0 to 3,(total number of people-1),
i want to make a function to pick out on person from this group, such that everytime i call a function with a group with 4 people, person with no 2 will have 75% chance of appearing.
i hope this clears it.

Sign in to comment.


Image Analyst
Image Analyst on 17 Jun 2012
It won't be, unless you change your array so that 3 out of the 4 elements have the value 2. If you don't do that the probability that you randomly select the one and only 2 would be 1/4 = 25%.

Image Analyst
Image Analyst on 17 Jun 2012
Based on your comment to the cyclist, try this:
personHist = zeros(1,4);
% Do a Monte Carlo simulation.
numberOfExperiments = 100000;
for experiment = 1 : numberOfExperiments
randomNumber = rand(1);
if randomNumber < 0.75
person = 2;
elseif randomNumber < 0.75 + 0.25/3
person = 1;
elseif randomNumber < 0.75 + 2*0.25/3
person = 3;
else
person = 4;
end
% Calculate histogram
personHist(person) = personHist(person) + 1;
end
% Normalize.
personHist = 100 * personHist / sum(personHist);
bar(personHist);
xlabel('Person Number');
ylabel('Percentage');
grid on;
% Print out to command window:
personHist
Essentially what you want is that "if" block in the inside of the Monte Carlo for loop.

Steven Lord
Steven Lord on 13 May 2021
This uses several functions that didn't exist when the question was originally asked in 2012, notably discretize, histogram, and yline. It also uses string arrays to create the horizontal lines.
values = [3 6 10 15];
probabilities = [0.75 0.19 0.05 0.01];
cumulativeProbabilities = cumsum([0 probabilities]);
% Discretize the uniform random numbers generated by rand into the bins in
% cumulativeProbabilities
generatedValues = discretize(rand(1, 1e5), cumulativeProbabilities, values);
% Display the results as a histogram
histogram(generatedValues, 'Normalization', 'probability')
% Show horizontal lines at each probability value to see how well the
% actual probabilities agree with the theoretical probabilities
for whichBin = 1:numel(values)
yline(probabilities(whichBin), ':', "value x = " + values(whichBin));
end
The heights of the bins are in close agreement to the probabilities in the probabilities vector.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!