Applying probability concept using matlab
조회 수: 2 (최근 30일)
이전 댓글 표시
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!
댓글 수: 0
답변 (4개)
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).
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%.
댓글 수: 0
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;
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.
댓글 수: 0
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));
end
The heights of the bins are in close agreement to the probabilities in the probabilities vector.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!