MATLAB: I want to see a 1000 results in a Histogram

for loophistogramMATLABvectorvectorization

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.

Best Answer

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
%