I want to see a 1000 results in a Histogram

조회 수: 1 (최근 30일)
Thomas Verhaegen
Thomas Verhaegen 2016년 12월 2일
편집: Thomas Verhaegen 2016년 12월 4일
I have created a script. I wish to loop this a 1000 times and place the result in a Histogram. Below is the script.
k= min(10,randi([1,13]));
a=0;
while k < 17
r = min(10,randi([1,13]));
if r==1
a= a+1;
r=11;
k=k+r;
else
k=k+r;
end
if k>21 && a > 0
k = k - 10;
a=a-1;
end
end
k
I can run the script as a loop, but that does not help me at all with creating a Histogram.

채택된 답변

Image Analyst
Image Analyst 2016년 12월 2일
Try this. Put all the code below (both functions) into one m-file, say, test1.m
function test1
% Initialization / clean-up code.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
% Call Thomas's code 1000 times, then histogram it.
for kk = 1 : 1000
k(kk) = ComputeK(kk);
end
histogram(k);
grid on;
title('Histogram of k', 'FontSize', fontSize);
xlabel('k', 'FontSize', fontSize);
ylabel('Counts', 'FontSize', fontSize);
% Some function to do stuff but
% appears to generate k as the output.
function k = ComputeK(k)
k = min(10,randi([1,13]));
a = 0;
while k < 17
r = min(10, randi([1, 13]));
if r == 1
a = a+1;
r=11;
k=k+r;
else
k=k+r;
end
if k > 21 && a > 0
k = k - 10;
a = a-1;
end
end
% k
%
  댓글 수: 3
Image Analyst
Image Analyst 2016년 12월 4일
If you want k to just do from 1 to 10, use a for loop instead of a while loop:
for k = 1 : 10
Thomas Verhaegen
Thomas Verhaegen 2016년 12월 4일
편집: Thomas Verhaegen 2016년 12월 4일
function Opdrachtc
clear;
clc;
fontSize = 15;
for kk = 1 : 10
h(kk) = MDealer(kk)
H=categorical(h,[1,2,3,4,5,6,7,8,9,10]);
end
histogram(H,'FaceColor','k','Edgecolor','r','LineWidth',2);
title('BlackJack', 'FontSize', fontSize);
fontSize=12;
xlabel('Mogelijkheden', 'FontSize', fontSize);
ylabel('Kans op 1000', 'FontSize', fontSize);
function k= MDealer(~)
% Berekenen van mogelijke kaarten van de dealer
% Een kaart met waarde tussen 1 en 10 wordt toegevoegd, zolang de waarde
% van de kaarten onder de 17 blijft. Zolang men niet boven de 21 komt
% zal een 1 tellen als een 11. Indien men boven de 21 komt zakt de waarde
% terug van 11 naar 1.
for l=1:10
k=l ;
a = 0;
if k == 1
k=11;
a=a+1;
end
while k < 17
r = min(10, randi([1, 13]));
if r == 1
a = a+1;
r=11;
k=k+r;
else
k=k+r;
end
if k > 21 && a > 0
k = k - 10;
a = a-1;
end
end
if k>21 && l==1
k=1;
elseif k>21 &&l==2
k=2;
elseif k>21 && l==3
k=3;
elseif k>21 && l==4
k=4;
elseif k>21 &&l==5
k=5;
elseif k>21 && l==6
k=6;
elseif k>21 && l==7
k=7;
elseif k>21 &&l==8
k=8;
elseif k>21 && l==9
k=9;
elseif k>21 && l==10
k=10;
else k=0;
end
x(l)=k;
end
k=x;
k

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by