error when calculating factorial

2 Ansichten (letzte 30 Tage)
Josep
Josep am 12 Aug. 2014
Kommentiert: Josep am 12 Aug. 2014
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?

Akzeptierte Antwort

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

Weitere Antworten (1)

Ben11
Ben11 am 12 Aug. 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 Kommentare
Ben11
Ben11 am 12 Aug. 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 am 12 Aug. 2014
that works now, everything okay! Thank you ;)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by