I have a schooltask where I'm about to do a function whom calculate how many thows its need to get yatzy with five dices.
I have done it pretty good I think, but with 50000 results I get the average throws to ~12 when it should be 191283/17248=*11.0902*
What have I done bad? I have looked over it alot and cant find anything wrong.
function [nr_throws] = nr_throws()
throw=[1,5]; % Matrix for 5 dices
nr_throws=1; % First throw
most_common=mode(throw); % Most occured dice in the throw
for dice=1:5
throw(dice)=ceil(6*rand); % Randomize the first throw
end
while max(std(throw))~=0 % Controll that the first throw not is yatzy
for dice=1:5
if throw(dice)~=most_common
throw(dice)=ceil(6*rand); % Randomize the throws that don't occur the most times
end
end
nr_throws=nr_throws+1; % New throw done
most_common=mode(throw); % Controlls the most occured throw again
end
Calculate the mean with:
function [mean] = mean()
i=1;
up_to=50000;
value=0;
while i <= up_to
value=value+nr_throws();
i=i+1;
end
mean=value/up_to;

 채택된 답변

Matt Tearle
Matt Tearle 2011년 5월 9일

0 개 추천

Try this:
function [nr_throws] = nr_throws()
nr_throws=1; % First throw
throw = randi(6,1,5);
most_common=mode(throw);
while max(std(throw))~=0 % Controll that the first throw not is yatzy
nmc = (throw~=most_common);
throw(nmc) = randi(6,1,nnz(nmc));
nr_throws=nr_throws+1; % New throw done
most_common=mode(throw); % Controlls the most occured throw again
end
I get a mean of 11.mumble. Your problem was in the first few lines. throw = [1,5] makes a 1-by-2 vector ([1,5]), which meant that most_common was always coming out as 1. Then you were actually doing the first throw. And, BTW, you can generate multiple random numbers in a single line -- no need for a loop.
The stuff inside the while loop was working, I think. I just cleaned it up with some logical indexing. As I do.

댓글 수: 7

Thomas Söderström
Thomas Söderström 2011년 5월 9일
Thank you it helped! I have another question. I wanna plot all the results on a histogram, but my code dosen't quite work. Can you see anything wrong? I also wanna calculate the mean of "throws" with MEAN, but that dosent seems to work eather.
function [experiment, average] = experiment(nr_experiment)
throws=[1,nr_experiment];
for counter=1:nr_experiment
throws(counter)=nr_throws();
end
average=mean(throws);
experiment=bar(hist(throws,1:1:max(throws)));
Matt Tearle
Matt Tearle 2011년 5월 9일
What problem are you having? It seems to work fine for me.
BTW, same issues as before: [1,nr_experiment] creates a 2-element array. Use zeros(1,nr_experiment) to initialize an array. And using experiment for both the function name and variable name is not a good practice.
Thomas Söderström
Thomas Söderström 2011년 5월 9일
When I run this code I dosen't get a value on the average as ans. It doesent even create any bars as a histogram should?
For exampel, when I run experiment(4) in Command Windows I get the ans = 1 0 0 1 (wow that was lucky!!). As you se, no bars and no mean.
Matt Tearle
Matt Tearle 2011년 5월 9일
Are you sure you've saved the version you posted above? The output you get looks like what you'd get if you didn't have the "bar" command in the last line (just "experiment = hist(...)").
Or maybe you have multiple files called experiment.m? Do
which experiment -all
to confirm. I did a copy/paste/save of the code you posted, and it works fine. The first output is the handle to the bar plot, the second is the value of the mean (around 11).
Thomas Söderström
Thomas Söderström 2011년 5월 9일
Yes I saved the version above. And I only have one experiemt file.
Did a PS on the outpost: http://i51.tinypic.com/2hcq4h0.jpg
As you can see, no mean value in the outpost.
Thomas Söderström
Thomas Söderström 2011년 5월 9일
EDIT: Same "error" when I use throws=zeros(1,nr_experiment) insteed of throws=[1,nr_experiment]
Matt Tearle
Matt Tearle 2011년 5월 10일
OK, the picture shows the expected output (this is different to the "1 0 0 1" output you mentioned before). When you call experiment(1000), you're not asking for any outputs, so MATLAB assigns one output to the default (ans). This means you get only the first of the multiple outputs returned by experiment. To get both, call with explicit output variables:
[x,y] = experiment(1000)
x will be the handle to the plot, y will be the mean.
Also, zeros() versus [1,nr_exp] won't change the behavior, just the internal workings. The loop overwrites throws anyway. The point of using zeros is to allocate space for throws before entering the loop, so that MATLAB doesn't have to resize/reallocate space inside the loop (which is inefficient).

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

추가 답변 (1개)

Sean de Wolski
Sean de Wolski 2011년 5월 9일

0 개 추천

Don't overwrite the builtin function mean!!! And don't overwrite the function mean (that you just created) with a variable mean!!! (maybe one more exclamation point?)
function [the_mean] = getMean()
i=1;
up_to=50000;
value=0;
while i <= up_to
value=value+nr_throws();
i=i+1;
end
the_mean=value/up_to;
I don't know if this is your problem or not, but let me know if it fixes it.

댓글 수: 3

Matt Tearle
Matt Tearle 2011년 5월 9일
Ditto nr_throws -- don't use a variable with the same name as the function. (repmat('!',1,5))
Also... for is neater than while:
up_to = 50000;
value = 0;
for i=1:up_to
value = value + nr_throws;
end
the_mean = value/up_to;
If you just want the mean, that will do, but it might be more useful right now (ie while debugging) to keep the values themselves:
throwvec = zeros(up_to,1);
for i=1:up_to
throwvec(i) = nr_throws;
end
the_mean = mean(throwvec);
% hist(throwvec), etc goes here for further analysis
Sean de Wolski
Sean de Wolski 2011년 5월 9일
(repmat('!',1,5))
I think you have preemptively won the award for today's best answer.
Matt Tearle
Matt Tearle 2011년 5월 9일
I'm all about scalability.

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

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by