Results 1 to 3 of 3

Math Help - Matlab Vector Factorial Function

  1. #1
    Newbie
    Joined
    Jan 2010
    Posts
    6

    Matlab Vector Factorial Function

    Hi there,

    I'm in a thermal physics course this term where we have had a very...short...introduction to Matlab to say the least.

    I have to write a simple code for positive integers (assumed) to calculate the factorial of an array, for example, so that I can plot it against the integer value.

    My code so far is:

    function z = n_fact(N);
    z = N;
    x = 0;
    for count=2:N
    x =(N-count+1);
    z = x.*z;
    end

    It can only accept integers...how can I get it so that it iterates individual values of an arary into the function?

    Any help would be very much appreciated. Thanks!
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by redherring View Post
    Hi there,

    I'm in a thermal physics course this term where we have had a very...short...introduction to Matlab to say the least.

    I have to write a simple code for positive integers (assumed) to calculate the factorial of an array, for example, so that I can plot it against the integer value.

    My code so far is:

    function z = n_fact(N);
    z = N;
    x = 0;
    for count=2:N
    x =(N-count+1);
    z = x.*z;
    end

    It can only accept integers...how can I get it so that it iterates individual values of an arary into the function?

    Any help would be very much appreciated. Thanks!
    First your function is horribly over complicated

    For a scalar try:

    Code:
    function  rv=CBfak(N)
      rv=1;
      for idx=2:N
        rv=rv*idx;  %* and .* are equivalent here since both operands are scalar
      end
    Now write a vector wrapper

    Code:
    function rv=CBVecFak(N)
      ll=length(N);
      rv=ones(size(N));
      for idx=1:ll
        rv(idx)=CBfak(N(idx));
      end
    You will have to test this code because I haven't


    CB
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Jan 2010
    Posts
    6
    Thank you very much! That works nicely
    Thanks again.

    RH
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Is the factorial function decreasing?
    Posted in the Calculus Forum
    Replies: 3
    Last Post: April 21st 2010, 09:21 AM
  2. factorial moment generating function
    Posted in the Advanced Statistics Forum
    Replies: 3
    Last Post: December 5th 2009, 03:20 PM
  3. Matlab function with vector inputs and matrix outputs
    Posted in the Math Software Forum
    Replies: 3
    Last Post: August 6th 2009, 10:49 PM
  4. Replies: 0
    Last Post: August 5th 2009, 01:51 PM
  5. Factorial gamma function
    Posted in the Calculus Forum
    Replies: 3
    Last Post: November 5th 2008, 02:10 AM

Search Tags


/mathhelpforum @mathhelpforum