我一直在努力将此网格搜索代码从 Fortran 转换为 Matlab,但是我无法正确合并 GO TO 语句(我正在尝试使用 while 循环,但我认为我需要其他东西来结束搜索) 。任何帮助将不胜感激。
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 语句实现两种类型的控制流:
类型1:跳出循环
GOTO 语句用于跳出 DO 循环:
DO acc0 = 1,intA+1
! some code
IF (cons<0.0) GOTO 102
! some more code
END DO
102 continue
正如你所注意到的,如果cons < 0.0
GOTO 语句指出移动到位于 DO 循环外部的标签 102。在matlab中无非就是一个简单的break
在 for 循环中:
for acc0=1:intA+1
% some matlab code
if (cons < 0.0)
break
end
% some more matlab code
end
类型2:创建循环
虽然没有显式编写循环,但以下代码创建了一些可以转换为 while 循环的内容:
! 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)