Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Factorial Function in Matlab?

20 views
Skip to first unread message

Naliberal

unread,
Jan 30, 1993, 12:16:26 AM1/30/93
to
Is there a factorial function in either the professional or
student edition of Matlab?? I have my student manual here and
I could not find a factorial function. If there is one, what
command do I use??

thanks
nabeel
P********************Palestinian Statehood Now!*****************************
E Nabeel Ibrahim | *
A ibr...@mentor.cc.purdue.edu | Oh, if humans could only *
C Electrical Engineering | multitask... *
E Purdue University | *
!**********"Racist, Sexist, Anti-gay, Operation Rescue, Go Away!"***********

Cleve Moler

unread,
Jan 30, 1993, 10:25:09 AM1/30/93
to
(Whew, at last an easy one.)

MATLAB does not have a factorial function, so n! must be computed by

prod(1:n)

or, possibly with roundoff error, by

gamma(n+1)

But, be careful, either of these quantities overflow for n > 170.
If you actually want n! to compute something like

n!/(k!*(n-k)!)

for large n, you should use

exp(sum(log(1:n))-sum(log(1:k))-sum(log(1:(n-k))))

It's not as slow as it looks, and it doesn't overflow.

-- Cleve Moler

Naliberal

unread,
Jan 31, 1993, 12:48:53 PM1/31/93
to
mo...@MathWorks.COM (Cleve Moler) writes:
>MATLAB does not have a factorial function, so n! must be computed by
> prod(1:n)

>If you actually want n! to compute something like


> n!/(k!*(n-k)!)
>for large n, you should use
> exp(sum(log(1:n))-sum(log(1:k))-sum(log(1:(n-k))))

I tried both of the above methods, but they don't seem to work
in loops. Specifically, I was trying to compute n!/(k!*(n-k)!)
for values of k from 0 to 10 while holding n at a constant
value of 10. Each time, n!/(k!*(n-k)!) was only computed
once. Any suggestions on how to fix this??

Thanks

0 new messages