zq13554337066
zq13554337066
2015-10-13 12:29
采纳率: 100%
浏览 2.3k
已采纳

fortran如何生成高斯矩阵

fortran中生成的一个随机矩阵如何转换成一个均值为0标准差为2的高斯矩阵

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • devmiao
    devmiao 2015-10-13 13:25
    已采纳
    点赞 评论
  • wangyaninglm
    shiter 2015-10-13 13:23
    Module ran_mod
      Implicit None
    ! ran return a uniform random number between 0-1  
    ! norma return a normal distribution  
    contains 
      function ran()   !returns random number between 0 - 1  
        implicit none 
        integer , save :: flag = 0
        double precision :: ran 
        if(flag==0) then 
          call random_seed()
          flag = 1 
        endif 
        call random_number(ran)     ! built in fortran 90 random number function  
      end function ran
    
      function normal(mean,sigma) 
        implicit none 
        integer :: flag 
        double precision, parameter :: pi = 3.141592653589793239  
        double precision :: u1, u2, y1, y2, normal, mean, sigma 
        save flag 
        data flag /0/ 
        u1 = ran(); u2 = ran() 
        if (flag.eq.0) then 
          y1 = sqrt(-2.0d0*log(u1))*cos(2.0d0*pi*u2) 
          normal = mean + sigma*y1 
          flag = 1 
        else 
          y2 = sqrt(-2.0d0*log(u1))*sin(2.0d0*pi*u2) 
          normal = mean + sigma*y2 
          flag = 0 
        endif  
      end function normal 
    !The above codes are made in Fortran 90 language, if you have any question, you may write to sealin2008@hotmail.com 
    End Module ran_mod
    
    Program www_fcode_cn
      use ran_mod
      Implicit None
      Integer , parameter :: N = 10000
      Real( Kind = 8 ) :: a( N )
      integer :: i
      Open( 12 , File = 'out.txt' )
      Do i = 1 , N
        a( i ) = normal( 5.0D0 , 2.0D0 )
        write( 12 , * ) a(i)
      End Do
      Close( 12 )
    End Program www_fcode_cn
    
    

    上述代码可以生成符合高斯分别的数据,矩阵的话,你得做个约束就行

    点赞 评论
  • devmiao
    devmiao 2015-10-13 13:26

    Module ran_mod
    Implicit None
    ! ran return a uniform random number between 0-1

    ! norma return a normal distribution

    contains
    function ran() !returns random number between 0 - 1

    implicit none
    integer , save :: flag = 0
    double precision :: ran
    if(flag==0) then
    call random_seed()
    flag = 1
    endif
    call random_number(ran) ! built in fortran 90 random number function

    end function ran

    function normal(mean,sigma)
    implicit none
    integer :: flag
    double precision, parameter :: pi = 3.141592653589793239

    double precision :: u1, u2, y1, y2, normal, mean, sigma
    save flag
    data flag /0/
    u1 = ran(); u2 = ran()
    if (flag.eq.0) then
    y1 = sqrt(-2.0d0*log(u1))*cos(2.0d0*pi*u2)
    normal = mean + sigma*y1
    flag = 1
    else
    y2 = sqrt(-2.0d0*log(u1))*sin(2.0d0*pi*u2)
    normal = mean + sigma*y2
    flag = 0
    endif

    end function normal
    !The above codes are made in Fortran 90 language, if you have any question, you may write to sealin2008@hotmail.com
    End Module ran_mod

    Program www_fcode_cn
    use ran_mod
    Implicit None
    Integer , parameter :: N = 10000
    Real( Kind = 8 ) :: a( N )
    integer :: i
    Open( 12 , File = 'out.txt' )
    Do i = 1 , N
    a( i ) = normal( 5.0D0 , 2.0D0 )
    write( 12 , * ) a(i)
    End Do
    Close( 12 )
    End Program www_fcode_cn

    点赞 评论
  • wzj_willis
    wzj_willis 2015-12-29 18:09

    我遇到了同样的问题,请问一下您的问题怎么解决的

    点赞 评论

相关推荐