Results 1 to 3 of 3

Math Help - Matlab--Problem with randn generation

  1. #1
    Junior Member
    Joined
    May 2008
    Posts
    50

    Matlab--Problem with randn generation

    I wrote a function and a top level script that queries and will return n random numbers that are normally distributed with mean m and standard deviation s.

    Here is my function and top level script:

    Function:

    function[gaussian]=my_gaussian(m,s,n);

    gaussian=randn(1,n.*s)+m


    Top Level Script:

    m=input('Enter the mean of your gaussian random numbers:');
    s=input('Enter the standard deviation of your gaussian random numbers:');
    n=input('Enter the number of gaussian random numbers:');

    [gaussian]=my_gaussian(m,s,n);



    Yet when I test the top level script with this:
    m=5
    s=2
    n=500
    this program then spits out 1000 numbers instead of 500. Why?

    Thanks,

    Kim
    Follow Math Help Forum on Facebook and Google+

  2. #2
    MHF Contributor

    Joined
    Aug 2008
    From
    Paris, France
    Posts
    1,174
    Quote Originally Posted by Kim Nu View Post
    I wrote a function and a top level script that queries and will return n random numbers that are normally distributed with mean m and standard deviation s.

    Here is my function and top level script:

    Function:

    function[gaussian]=my_gaussian(m,s,n);

    gaussian=randn(1,n.*s)+m
    The problem is on this last line: you're asking for a 1-column and n\times s-lines matrix of random numbers.
    Whereas what you needed is a 1-column and n-lines matrix of random numbers, multiplied by s. That is to say:
    Code:
    gaussian=s*randn(1,n)+m
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Junior Member
    Joined
    May 2008
    Posts
    50
    Thanks, I thought it was something minor like this. Thanks for all your help.

    Kim
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Polynomial Generation
    Posted in the Advanced Algebra Forum
    Replies: 1
    Last Post: May 5th 2010, 05:34 AM
  2. grid generation
    Posted in the Math Software Forum
    Replies: 1
    Last Post: January 30th 2010, 03:06 PM
  3. sequence generation
    Posted in the Algebra Forum
    Replies: 2
    Last Post: October 6th 2009, 07:04 AM
  4. MATLAB - Random Number Generation
    Posted in the Math Software Forum
    Replies: 4
    Last Post: February 18th 2009, 07:03 AM
  5. Multiple array generation in Matlab
    Posted in the Math Software Forum
    Replies: 1
    Last Post: January 29th 2009, 03:01 PM

Search Tags


/mathhelpforum @mathhelpforum