Hi,
I'm a beginner at matlab and a beginner in coding, so I was wondering if people could help me out with this problem that I'm experiencing. Basically I need to graph the probability distribution for sum of 16 rolls of a fair die, and so how I coded it was that I made 16 for-loops, however the code is taking too long to run and so I feel like I probably have to make my code more "efficient" except I'm not sure how I should go about doing this, like what function to use or how to code it.
I've attached my code for how I code the prob distr. for 8 rolls of a fair die. I basically used the same for and just added 8 more for loops, but the program is taking way to long to code, so I'm not sure how I can code it to make it run faster/actually run.
DValues = 1:6; %dice values
DTotal = 8:48;Probsum = zeros(1, length(DTotal))for x = DValues %Roll 1%
for y = DValues %Roll 2%
for z = DValues %Roll 3%
for w = DValues %Roll 4%
for a = DValues %Roll 5%
for b = DValues %Roll 6%
for c = DValues %Roll 7%
for d = DValues %Roll 8%
T = x + y + z+ w + a + b+ c + d; for index = 1:length(DTotal) if T==DTotal (index) Probsum(index) = Probsum(index) + 1; end end end end end end end end endendProbsumplot(DTotal,Probsum/65536)title('Probability Distribution for Sum of 8 Rolls of A Fair Die')ylabel('Probability')xlabel('Sum')
Best Answer