Afficher commentaires plus anciens
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;
1 commentaire
Kishor
le 20 Août 2011
see MATLAB help examples on nargin.it is number input argument to your functions(count).
Réponse acceptée
Plus de réponses (4)
lasse
le 19 Août 2011
0 votes
6 commentaires
Sean de Wolski
le 19 Août 2011
Why don't you ask him (her)? It's his (her) logic.
lasse
le 19 Août 2011
Sean de Wolski
le 19 Août 2011
I understand it, yes, but that's not helping you. For you to understand it you have to figure it out yourself. Use the debugger, put a break point on the first line and work through it step by step and see what's happening.
lasse
le 19 Août 2011
Sean de Wolski
le 19 Août 2011
Then finding a computer that has ML should be your first step. Perhaps at the school's library/computer cluster etc.?
lasse
le 19 Août 2011
Fangjun Jiang
le 19 Août 2011
0 votes
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.
9 commentaires
lasse
le 19 Août 2011
Sean de Wolski
le 19 Août 2011
And, assuming it's the MB game, it's misspelled!
Walter Roberson
le 19 Août 2011
Since you know that the condition will always be true, you can remove the "if" and the "else" statement, reducing it down to
k = 1:throw_max;
p = p*n;
As to what that means in the context of the code... Good luck understanding it, as it involves stochastic transition matrices which is a topic in Mathematics, not in MATLAB.
Sean de Wolski
le 19 Août 2011
And the assumption that n is a number and not an OOP object.
Fangjun Jiang
le 19 Août 2011
@Sean, you got keyboard latency too?!
Fangjun Jiang
le 19 Août 2011
I am simply explaining the syntax of the code here.
Check the value of throw_max, k=1:throw_max is to create a vector of 1,2,3,4,...,throw_max
p=p*n, p is a vector, it multiple every element of p with n. Try an example separately in command window. p=1:5;n=3;p=p*n;
Oleg Komarov
le 19 Août 2011
The OP doesn't have matlab but yet he wrote the code of his friend.
lasse
le 19 Août 2011
Oleg Komarov
le 19 Août 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.
Daniel Shub
le 20 Août 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.
2 commentaires
hanisah bt azhar
le 29 Nov 2018
Modifié(e) : hanisah bt azhar
le 29 Nov 2018
why it said that support for 'nargin' in the script has been removed in my matlab?
Steven Lord
le 29 Nov 2018
Because it has been. You can no longer call nargin, nargout, or inputname from within a script as stated in the Release Notes for release R2016b. You can still call them from within a function or a class method.
Scripts can't have inputs so they can't have a number of inputs.
Scripts can't have inputs so those (non-existent) inputs can't have names.
Scripts can't return outputs so they can't have a number of outputs.
Aldo Díaz
le 28 Avr 2020
0 votes
nargin works fine except for nargin < 3, it seems there is a bug, at least on R2018b!!!!
1 commentaire
Walter Roberson
le 29 Avr 2020
Could you give us an example of nargin failing for you in R2018b ?
Catégories
En savoir plus sur Get Started with MATLAB dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!