本文介绍了转到语句-Fortran到Matlab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力地将网格搜索代码从Fortran转换为Matlab,但是我无法正确合并GO TO语句(我试图使用while循环,但我认为我还需要其他一些东西来结束它们搜索).任何帮助将不胜感激.

I have been trying really hard to convert this grid search code from Fortran to Matlab, however I am not able to properly incorporate the GO TO statements (I am trying to use while loops but I think I need something else for them end the search). Any help would be greatly appreciated.

vmax    = -1.0E+15
amax_G  = -1

askipR  = REAL(ac_max - ac_min)/REAL(intA)

askip   = CEILING(askipR)


DO acc0 = 1,intA+1

acc = ac_min + askipR * (acc0-1)

    cons = ( x1 - grida2(acc) ) / onetauc

    IF (cons<0.0) GOTO 102

    vtemp = utilR(cons) + one_sv * utilB(acc)

    IF (vtemp>vmax) THEN
        vmax = vtemp
        amax_G = acc
    ELSE
        GOTO 102
    ENDIF

ENDDO ! acc0


102 continue

IF (askip<2) GO TO 109

askip = askip/2

IF (amax_G>ac_min) THEN

    acc = amax_G - askip
    cons = ( x1 - grida2(acc) ) / onetauc

    IF (cons<0.0) PRINT *,'WARNING: NEGATIVE CONS @ ASEARCH_Rx'

    vtemp = utilR(cons) + one_sv * utilB(acc)

    IF (vtemp>vmax) THEN
        vmax   = vtemp
        amax_G = acc
        GOTO 102
    ENDIF

ENDIF


IF (amax_G < ac_max) THEN

    acc  = amax_G + askip
    cons = ( x1 - grida2(acc) ) / onetauc

    IF (cons<0.0) GO TO 102

    vtemp = utilR(cons) + one_sv * utilB(acc)

    IF (vtemp>vmax) THEN
        vmax = vtemp
        amax_G = acc
    ENDIF

ENDIF

GOTO 102

109 CONTINUE

vfunR(jc,ac,sc)   = vmax    ! jc=Nj

afunR_G(jc,ac,sc) = amax_G  ! jc=Nj

推荐答案

您使用GOTO语句实现了两种控制流:

You have two types of control-flow implemented with your GOTO statement:

类型1:突围

GOTO语句用于打破DO循环:

The GOTO statement is used to break out of the DO-loop:

    DO acc0 = 1,intA+1
        ! some code
        IF (cons<0.0) GOTO 102
        ! some more code
    END DO
102 continue

您注意到,如果 cons<0.0 GOTO语句指出要移至标签102,该标签位于DO循环之外.在matlab中,无非是for循环中的简单 break :

As you notice, if cons < 0.0 the GOTO statement states to move to label 102 which is located just outside of the DO-loop. In matlab is nothing more than a simple break in a for-loop:

for acc0=1:intA+1
  % some matlab code
  if (cons < 0.0)
    break
  end
  % some more matlab code
end

类型2:创建循环

虽然未明确编写循环,但以下代码创建了可以转换为while循环的东西:

While the loop is not explicitly written, the following code creates something that can be translated as a while loop:

    ! This is the start of the loop
102 continue
    ! This is the condition to exit the loop <<==
    IF (askip<2) GO TO 109
    ! stuff
    ! This is a condition to restart the loop    <<==
    IF (vtemp>vmax) THEN
         vmax   = vtemp
         amax_G = acc
         GOTO 102
    ENDIF
    ! stuff
    ! This is another condition to restart the loop   <<==
    IF (cons<0.0) GO TO 102
    ! stuff
    ! This is the end of the loop, asked to restart it  <<==
    GOTO 102
    ! This is outside of the loop
109 CONTINUE

最后,您将其翻译为:

while (askip >=2)
   % stuff
   if (vtemp > vmax)
        vmax = vtemp
        amax_G = acc
        continue
   end
   % stuff
   if (cons < 0.0)
      continue
   end
   % stuff
end

这篇关于转到语句-Fortran到Matlab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 01:49