Fortran array with dynamic size, as easy the R function seq()

后端 未结 2 1112
渐次进展
渐次进展 2021-01-16 05:41

I would like to write Fortran code that works like the R function seq(). E.g.:

x <- seq(0,1,0.1)

will give the vector

x          


        
2条回答
  •  萌比男神i
    2021-01-16 05:53

    Fortran 2003 has (re-)allocation upon assignment for allocatable arrays, and the program

    program xgrid
    implicit none
    real, allocatable :: x(:)
    integer           :: i,n
    do n=5,10,5
       x = 0.1*[(i,i=0,n)]
       write (*,"('x =',100(1x,f0.1))") x
    end do
    end program xgrid
    

    compiled with gfortran 4.8.0, shows a Fortran one-liner equivalent to seq(), giving output

    x = .0 .1 .2 .3 .4 .5

    x = .0 .1 .2 .3 .4 .5 .6 .7 .8 .9 1.0

提交回复
热议问题