【问题标题】:Fortran loop for discrete values离散值的 Fortran 循环
【发布时间】:2014-04-03 13:06:50
【问题描述】:

有没有办法为变量的离散值运行循环?最新版本呢?

有点像

for i in 1  5 9 11  31 77 

在 Unix shell 脚本中使用?

【问题讨论】:

  • 有人试图在fotran 中编写pascal 程序。
  • 我同意,应该可以说DO I=(/ 1,3,5,7 /),但你不能。也许可以在implicit do 中完成。我会调查的。
  • @ja72:您断言有人试图在 Fortran 中编写 Pascal 程序很奇怪,Pascal 没有比 Fortran 提供更多对任意数字列表的“循环”支持。此外,OP 表示她从 Unix shell 脚本中汲取了灵感。
  • @HighPerformanceMark in pascal,您可以使用非连续值循环 emum。但它们必须是常量。

标签: fortran fortran95


【解决方案1】:
integer, dimension (5) :: indx = [5, 9, 11, 31, 71]

do i=1, size(indx)
   j=indx(i)
   ....
end do

【讨论】:

    【解决方案2】:

    您也可以使用隐含的 do 循环来完成此操作,但您必须如上所述定义值数组:

    integer, dimension (5) :: indx = [5, 9, 11, 31, 71]
    integer, dimension (5) :: rslt 
    integer, external      :: func
    rslt = (/ func(indx(j)), j=1,5 /)
    

    【讨论】:

      【解决方案3】:

      我不确定这是否有帮助,但您可以将数组用于索引

      program Console1
      
      implicit none
      
      ! Variables
      INTEGER :: X(4) = (/ 1, 3, 5, 7 /)
      REAL :: Y(10) = 0.0
      
      ! Body of Console1
      print *, X
      !   1   3   5   7
      Y(X) = 10.0/X
      print *, Y
      !   10.0    0.0    3.33   0.0    2.00   0.0    1.428    0.0 ...
      
      end program Console1
      

      【讨论】:

        猜你喜欢
        • 2013-08-11
        • 2012-07-08
        • 2021-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-24
        • 1970-01-01
        • 2018-05-18
        相关资源
        最近更新 更多