error when calculating factorial

I want to calculate all the combinations for a X number of variables and I am doing this code:
m=6;%number of variables
TC=0;
for n=1:m,
C=factorial(m)/((factorial(n)*factorial(m-n));
TC=TC+C; %total number of combinations
end
TC
but I have the following error message from MATLAB:
Unbalanced or unexpected parenthesis or bracket.
Does anybody know which would be the right code to compute the total number of combinations, or where is the error in my code?

 Respuesta aceptada

Christoph
Christoph el 12 de Ag. de 2014
C=factorial(m)/((factorial(n)*factorial(m-n));
You are opening two parentheses, but closing only one in the denominator

Más respuestas (1)

Ben11
Ben11 el 12 de Ag. de 2014
The error is due to a missing bracket at the end of this line:
C=factorial(m)/((factorial(n)*factorial(m-n));
Add a bracket and it works.
C=factorial(m)/((factorial(n)*factorial(m-n)));

13 comentarios

Josep
Josep el 12 de Ag. de 2014
okay, I see the missing bracket but there is still an error..
Error in factorial (line 4)
C=factorial(m)/((factorial(n)*factorial(m-n)));
any idea?
Ben11
Ben11 el 12 de Ag. de 2014
what is the error message? I used your code with the added bracket and it worked.
Josep
Josep el 12 de Ag. de 2014
>> factorial Attempt to execute SCRIPT factorial as a function: C:\Users\p105312\Desktop\factorial.m
Error in factorial (line 4) C=factorial(m)/((factorial(n)*factorial(m-n)));
Ben11
Ben11 el 12 de Ag. de 2014
ok is "factorial" the name of your script? If so change it for something else like Factorial_function for instance, otherwise Matlab might confuse it with the built-in function "factorial".
Josep
Josep el 12 de Ag. de 2014
Editada: Josep el 12 de Ag. de 2014
so what you mean is that I have to save the file as a different name than factorial? Because I have already saved the file as fact.m and it still doesn't work.
Ben11
Ben11 el 12 de Ag. de 2014
yes, because there is already a built-in function named factorial.
Josep
Josep el 12 de Ag. de 2014
I don't know what I should do to make it works. Sorry for my lack of comprehension but I am new on Matlab.
Ben11
Ben11 el 12 de Ag. de 2014
No problem! You can simply save your m.file under a different name using the "save as" button.
Josep
Josep el 12 de Ag. de 2014
Editada: Josep el 12 de Ag. de 2014
Okay! the problem has been that I had to delete the file called factorial in my desktop, now it works perfectly!
Thank you very much Bill, you have been very kind and useful!
Ben11
Ben11 el 12 de Ag. de 2014
can you post your whole .m file?
Josep
Josep el 12 de Ag. de 2014
do you mean this?
Ben11
Ben11 el 12 de Ag. de 2014
yep. That's weird that it does not work if this is what you have in your m.file. Maybe restart matlab and run it again? Copying this code in your command window generates this error?
Josep
Josep el 12 de Ag. de 2014
that works now, everything okay! Thank you ;)

Iniciar sesión para comentar.

Categorías

Más información sobre Introduction to Installation and Licensing en Centro de ayuda y File Exchange.

Preguntada:

el 12 de Ag. de 2014

Comentada:

el 12 de Ag. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by