Asked by lasse
on 19 Aug 2011

Can anyone please tell me what if the nargin function is doing? I've been trying to learn how to use matlab on my own. //thanx

function [m_hat,s2hat ] = yatzy(n)

for m = 1:n value = tillsfem(5); throws(m) = value;

end m_hat = mean(throws); s2hat = var(throws);

throw_max = max(throws); p = []; A = [0 1/6 1/36 1/216 1/1296; 0 5/6 10/36 15/216 25/1296; 0 0 25/36 80/216 250/1296; 0 0 0 120/216 900/1296; 0 0 0 0 120/1296]; e1 = [1 0 0 0 0]'; e5 = [0 0 0 0 1]'; p = zeros(throw_max, n); for m = 1:throw_max pm = e1'*A^m*e5; p(m) = pm; end

if nargin < 2 k = 1:throw_max; p = p*n; else p = p*n*throw_max/k; end figure(1); clf; hist(throws,k); hold on stem(1:throw_max,p,'r') title('number of throws to get 5 equal dice') legend('Numerical','Analytical') xlabel('Number of throws') ylabel('Frequency') hold off

fprintf(['Expected number of throws to get: %4.2f '... '(analytical)\n' ... ' %4.2f '... '(numerical)\n' ... 'Expected variance: %4.2f '... '(analytical)\n' ... ' %4.2f '... '(numerical)\n'], ... 191283/17248, m_hat, 12125651655/297493504, s2hat);

I just need help with explaining if nargin < 2 k = 1:throw_max; p = p*n; else p = p*n*throw_max/k;

*No products are associated with this question.*

Answer by Sean de Wolski
on 19 Aug 2011

`nargin` means *"number of arguments in"*. In the above code it looks pretty useless:

if nargin < 2 %do stuff end

`nargin` in this case can only be 0 or 1, since yatzy(n) only allows for one input argument, (n), or no input arguments, yatzy. If you call yatzy with two input arguments, e.g.

yatzy(4,5)

It will error out with something along the lines of "Error using yatzy, more input arguments than expected"

Now as for future reference, since you're learning: nargin is typically used for error checking and so you can have optional arguments, e.g:

let's write a program `f` that takes one or two input arguments with the 2nd being optional:

function out = f(x,y); if nargin==0 %user called >>f error('Not enough input arguments, 1 is required'); elseif nargin==1 %user called f(x) y = pi; end %if neither, user called f(x,y)

Here we've made sure the person enters at least one argument, x, if they entered a second one great! if not, the second one, y, = pi.

Good luck!

Answer by lasse
on 19 Aug 2011

hi thank you for the answer, but could you explain the rest of the peace for me k = 1:throw_max; p = p*n; else p = p*n*throw_max/k; what k,n,p is? and what happens later i got this from a friend so is kinda hard for me to understand everything :)

Show 3 older comments

lasse
on 19 Aug 2011

i can tell you that ive tried to understand it for days now, but the problem is i dont have the matlab at home and this need to be done before school start. So if you can help i would be very thankful.

Sean de Wolski
on 19 Aug 2011

Then finding a computer that has ML should be your first step. Perhaps at the school's library/computer cluster etc.?

lasse
on 19 Aug 2011

thats the problem,i dont have it and i live far away from school. I have wrote a whole yatzy game code and this is the last part, so if you dont mind explaining either wise, thank you anyway. :)

Answer by Fangjun Jiang
on 19 Aug 2011

It is likely a bad code. The function has only one input argument so nargin<2 is always true. If you try to provide more than one input arguments like yatzy(3,4), it will cause an error saying too many input arguments.

Show 6 older comments

Oleg Komarov
on 19 Aug 2011

The OP doesn't have matlab but yet he wrote the code of his friend.

lasse
on 19 Aug 2011

i wrote this code before summer vacation at school, just went home from my real vacation thats why its kinda hard for me to understand everything now.

Oleg Komarov
on 19 Aug 2011

Then go to the getting started guide, chapter about matrix manipulation: http://www.mathworks.com/help/techdoc/learn_matlab/f2-8955.html. You'll find the answer in 5-15 minutes and you'll understand why your story is just a story.

Friends, homework, assignments it's for your benefit. Help yourself and others will help. If you asked WHERE could you find the explanation you would have saved yourself lots of time.

You could find useful: http://www.mathworks.com/matlabcentral/answers/6200-tutorial-how-to-ask-a-question-on-answers-and-get-a-fast-answer

Give a look to the other links as well.

Answer by Daniel Shub
on 20 Aug 2011

You do not need MATLAB to figure out what is going on. Just work through your code line by line with a pencil and paper.

function [m_hat,s2hat ] = yatzy(n)

At this point the only thing in memory is n.

for m = 1:n

now we do something n times. What do we do, well that is:

value = tillsfem(5);

Well is is not so clear since you haven't told us what tillsfem does. It looks like value could be anything. Oh wait, look,

throws(m) = value;

this means value must be a scalar. So we could actually write

throws(m) = tillsfem(5);

So now we have n and throws which is a nx1 array of something.

m_hat = mean(throws);

hmmm, I wonder what that does. Do you know what the command mean does? You can go to the online documentation and find out that mean takes the mean of an array and returns a scalar. So now you have n and m_hat which are a scalars and throws which is a nx1 array.

It is as easy as that. The nice about MATLAB is it does exactly what you tell it to do.

Related Content

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
## 1 Comment

## Kishor (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/14060#comment_31170

see MATLAB help examples on nargin.it is number input argument to your functions(count).