+ Post New Thread
Results 1 to 6 of 6
  1. #1
    Member level 2
    Points: 856, Level: 6

    Join Date
    Jan 2016
    Posts
    48
    Helped
    0 / 0
    Points
    856
    Level
    6

    Generate random sequence of 1's and -1's in matlab

    How do i generate a random sequence of 1's and minus -1's in matlab with mean 0?

    •   AltAdvertisement

        
       

  2. #2
    Advanced Member level 5
    Points: 15,719, Level: 30
    pancho_hideboo's Avatar
    Join Date
    Oct 2006
    Location
    Real Homeless
    Posts
    2,396
    Helped
    644 / 644
    Points
    15,719
    Level
    30

    Re: Generate random sequence of 1's and -1's in matlab

    Code:
    x0 = 1:40;
    y0 = 2*int32( randn( size(x0) ) >= 0.0 ) - 1;
    Also see https://www.edaboard.com/showthread.php?373245#6



    •   AltAdvertisement

        
       

  3. #3
    Member level 2
    Points: 856, Level: 6

    Join Date
    Jan 2016
    Posts
    48
    Helped
    0 / 0
    Points
    856
    Level
    6

    Re: Generate random sequence of 1's and -1's in matlab

    This doesnt always give a mean of zero



    •   AltAdvertisement

        
       

  4. #4
    Full Member level 5
    Points: 1,257, Level: 8

    Join Date
    Jan 2019
    Posts
    254
    Helped
    49 / 49
    Points
    1,257
    Level
    8

    Re: generate random 1's and -1's in matlab

    the rand function generates uniformly distributed random numbers

    if you tell it to generate random numbers between 0 and 1,
    you can convert those less than 0.5 to -1 and those greater than 0.5 to 1



  5. #5
    Super Moderator
    Points: 256,485, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    44,740
    Helped
    13608 / 13608
    Points
    256,485
    Level
    100

    Re: Generate random sequence of 1's and -1's in matlab

    This doesnt always give a mean of zero
    By nature of random distribution, an arbitrary selected sample can't have exactly zero mean otherwise it's not random.

    You may want to specify additional properties of the random distribution, e.g. maximal imbalance for a given sample length.



    •   AltAdvertisement

        
       

  6. #6
    Advanced Member level 5
    Points: 15,719, Level: 30
    pancho_hideboo's Avatar
    Join Date
    Oct 2006
    Location
    Real Homeless
    Posts
    2,396
    Helped
    644 / 644
    Points
    15,719
    Level
    30

    Re: Generate random sequence of 1's and -1's in matlab

    Quote Originally Posted by Chinmaye View Post
    This doesnt always give a mean of zero
    If length of random sequence is enough large, average is zero.



--[[ ]]--