Applying probability concept using matlab

23 ビュー (過去 30 日間)
NUR KHAIRUNNISA rahimi 2012 年 6 月 17 日
回答済み: Steven Lord 2021 年 5 月 13 日
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!

回答 (4 件)

the cyclist
the cyclist 2012 年 6 月 17 日
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 件のコメント
NUR KHAIRUNNISA rahimi 2012 年 6 月 17 日
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.


Image Analyst
Image Analyst 2012 年 6 月 17 日
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 2012 年 6 月 17 日
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;
person = 4;
% Calculate histogram
personHist(person) = personHist(person) + 1;
% Normalize.
personHist = 100 * personHist / sum(personHist);
xlabel('Person Number');
grid on;
% Print out to command window:
Essentially what you want is that "if" block in the inside of the Monte Carlo for loop.

Steven Lord
Steven Lord 2021 年 5 月 13 日
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));
The heights of the bins are in close agreement to the probabilities in the probabilities vector.


Community Treasure Hunt

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

Start Hunting!

Translated by