Using MINLOC with Fortran: Incompatible ranks 0 and 1 in assignment

前端 未结 2 880
北海茫月
北海茫月 2020-12-19 17:52

Version that gives an error message

program hello
   integer a(9)
   integer index; ! note no dimension here
   a=(/1, 3, 4, 5, 6, 7, 8, 9, 10/)
   index =         


        
相关标签:
2条回答
  • 2020-12-19 18:39

    You can fix the first version, obtaining a scalar return from minloc, by using the DIM argument:

    index = MINLOC(a, DIM=1, MASK=(a > 5))
    

    P.S. No need for semicolons to end statements unless you place multiple statements per line. Fortran isn't C.

    0 讨论(0)
  • 2020-12-19 18:44

    This discussion brings up an important point: MINLOC returns an array, even if it's only one number, it's still an array.

    It may be possible to declare index as an array, like mentioned above, or to use a temporary array.

    integer :: temp(1)
    ...
    temp=minloc(dist)
    index=temp(1)
    

    It is also possible to use DIM argument to avoid manual fiddling with data types, like mentioned in M.S.B's answer.

    0 讨论(0)
提交回复
热议问题