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?
채택된 답변
추가 답변 (1개)
Ben11
2014년 8월 12일
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
Josep
2014년 8월 12일
Ben11
2014년 8월 12일
what is the error message? I used your code with the added bracket and it worked.
Josep
2014년 8월 12일
Ben11
2014년 8월 12일
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".
Ben11
2014년 8월 12일
yes, because there is already a built-in function named factorial.
Josep
2014년 8월 12일
Ben11
2014년 8월 12일
No problem! You can simply save your m.file under a different name using the "save as" button.
Ben11
2014년 8월 12일
can you post your whole .m file?
Josep
2014년 8월 12일
Ben11
2014년 8월 12일
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
2014년 8월 12일
카테고리
도움말 센터 및 File Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!