将循环从Fortran转换为MATLAB [英] translating loop from Fortran to MATLAB

查看:188
本文介绍了将循环从Fortran转换为MATLAB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在将代码从Fortran手动转换为MATLAB,并且我不确定如何翻译它的一部分。 (整个代码实际上是一个2000行子程序。)代码如下。

  C系列交叉领口? 
120 neckext = x(低谷(peaknum-1))+
* dydx * real((t-trough(peaknum-1)))


if (x(t).lt.neckext)转到130
C NO。在这里,如果系列没有穿过领口,也没有新的槽找到
C检查是否找到新的槽。如果(t.ge.lastobs)转到900
if(x(t).lt.min),则
t = t + 1
然后
min = x(t)
mindate = t
end if
troughid = min *(1.0 + cutoff)
if(x(t).ge.troughid)转到150
转到120

C是。如果系列在新槽前发现
130 dblcount = 0
if(poscount.ge.1)然后
DO 132 i = 1,poscount
if(((enterdt( i)-2.le.t).and。(t.le.enterdt(i)+2))then
dblcount = dblcount + 1
end if
132 continue
如果(dblcount.ge.1)然后
C写(30,2583)t,Cutnum
2583格式('DoubleCounting情节发生在',I5,
*'Cutoff =' ,F3.1)
转到150
结束如果
结束如果

我的问题是这部分代码:

  if(x(t).ge.troughid)goto 150 
goto 120

当我在MATLAB中翻译这部分时,我写了如下内容:

  if x(t,:)> = troughid 
t = marker;
minimum = x(t,:);
end

但我不知道如何处理标签120.当我翻译它,我是否再次编写该部分?因为据我所知,当我回到120时,代码将再次运行。谢谢!



编辑:作为Chris对150和900标签的问题的回应,我会在这里发表。

  150 t = marker 
min = x(t)

这是900的标签。

  C最后一次观察发现。这个迭代完成。 
900继续


解决方案

到目前为止,Matlab不包含任何goto命令的变体。核心Matlab命令集似乎是围绕结构化编程的理念而设计的。 (如果我正确地记住了我的CS古代历史,这是面向对象编程之前的大讨论。)Wikipedia有一个体面的讨论结构化编程。在结构化编程之前的黑暗日子里,人们过去对流程图感到非常兴奋,因为那是其中之一使用大量 goto 语句(现在通常称为意大利面代码)来显示和理解一段代码的最简单方法。

我怀疑你需要对整个子程序进行流程图,然后决定哪个控制流程结构最适合用来重新创建你的代码。如果它是一个相对简单的图,那么如果语句或 case 语句,您应该可以使用重新创建整个代码,尽管一系列小帮手功能可能更优雅。如果它有更复杂的结构,那么翻译可能需要更多的创造力。

I'm currently translating code from Fortran to MATLAB manually, and I am unsure of how to translate part of it. (The entire code actually is a 2,000 line subroutine.) The code is below.

C  Has series crossed neckline?
120        neckext=x(trough(peaknum-1))+
 *              dydx*real((t-trough(peaknum-1)))


        if(x(t).lt.neckext) goto 130
C      NO.  Here if series has not crossed neckline, nor new trough found
C           Check to see if new trough has been found.
        t=t+1
        if(t.ge.lastobs) goto 900
        if(x(t).lt.min) then
              min=x(t)
              mindate=t
              end if
        troughid=min*(1.0+cutoff)
        if(x(t).ge.troughid) goto 150
        goto 120

C      YES. Here if series crossed neckline before new trough found
130         dblcount=0
      if(poscount.ge.1) then
          DO 132 i=1,poscount
           if((enterdt(i)-2.le.t).and.(t.le.enterdt(i)+2)) then
           dblcount=dblcount+1
           end if    
132          continue
           if(dblcount.ge.1) then
C                write(30,2583) t,Cutnum
2583            format('DoubleCounting episode occurred at ',I5,
 *             ' with Cutoff = ',F3.1)
            goto 150
          end if
       end if

My problem is with this part of the code:

        if(x(t).ge.troughid) goto 150
        goto 120

When I was translating this part in MATLAB, I was writing something like:

if x(t,:)>=troughid
    t=marker;
    minimum=x(t,:);
end

But I don't know what to do with the label 120. When I translate it, do I write that part again? Because from what I understand, when I go back to 120, the code will be running again. Thanks!

EDIT: As a response to Chris's question on what labels 150 and 900 do, I'll post them here.

150        t=marker
           min=x(t)

And this is for the label 900.

C  Last observation found.  This iteration finished.
900        continue

解决方案

As it should be clear by now, Matlab does not include any variant of a "goto" command. The core Matlab command set appear to be designed around the "structured programming" philosophy. (Which, if I remember my CS ancient history correctly, was the great debate prior to object oriented programming.) Wikipedia has a decent discussion of structured programming.

In the dark days before structured programming, people used to be very excited about flow charts, since that was one of the easiest ways to visualized and understand a piece of code using a lot of goto statements (now usually referred to as spaghetti code).

I suspect that you will need to flowchart the entire subroutine, and then decide which control flow constructs can best be used to recreate your code. If it is a relatively simple diagram, then you should be able to recreate the entire code with if statements or case statements, although a series of small helper functions may be more elegant. If it has a more complex structure, then it may take a little more creativity to translate.

这篇关于将循环从Fortran转换为MATLAB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆