Implicit do loop array initialization

前端 未结 2 1238
囚心锁ツ
囚心锁ツ 2021-01-04 10:59

I want to initialize an array on one line with an implicit do loop. However, I always get a syntax or shape error. Can anyone help me correct the following construc

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-04 11:20

    The implicit do loop will only create a vector so you'll have to reshape that. Something like this:

    integer, dimension(m,n) :: myarray
    integer :: ix, jx
    ...
    myarray = reshape( [ (ix, ix = 1, m*n) ], [ m, n ] )
    

    or perhaps you want a more complicated, nested, implied-do loop:

    myarray = reshape( [ ((ix+jx, ix = 1, m), jx = 1, n) ], [ m, n ] )
    

    Note that I'm using the Fortran2003 convention of [ ] to delimit array constructions, rather than (/ /). Note also that you have to declare the implied do loop index variables.

提交回复
热议问题