八度GNU:未定义变量'x',即使已将其定义为函数输入 [英] Octave GNU: Undefined variable 'x' , even though it's defined as function input

查看:90
本文介绍了八度GNU:未定义变量'x',即使已将其定义为函数输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是试图编写一个简单的程序来查找n个数字的gcd.我不知道如何解决此错误,我已经阅读了所有八度音阶函数文档,并试图找到类似这样的问题...刚开始在八度音阶进行编程. 这是代码:

just trying to write a simple program to find the gcd of n numbers. I have no clue how to fix this error, i've read all of octaves function documentation and tried to find questions like this ... Just started programming in Octave btw. Here's the code:

function divisor = gcd(x, y)
  q=0;
  r=0;
  l=0;
  h=0;
  if(x>y)
    h=x;
    l=y;
 elseif(x<y) 
    h=y;
    l=x;
 else
    h=y;
    l=x;
 endif 
 while(r != 0)
    q=floor(h/l);
    r = h-l*q;
    q=h;
    r=l;
 endwhile
 divisor = q;
 printf("%d", q);
 return;
endfunction

错误:

error: 'x' undefined near line 6 column 6
error: called from
     gcd at line 6 column 3

谢谢:)

推荐答案

您的代码是一个函数定义.您的函数称为gcd.

Your code is a function definition. Your function is called gcd.

您必须将代码保存在名为gcd.m的文件中,然后创建一个新文件,以便可以从中调用该函数.

You have to save your code in a file called gcd.m and then create a new file so that you can call that function from it.

在与保存gcd.m相同的目录中,创建一个文件(例如:gcdtest.m),并将以下代码放入其中:

In the same directory you have saved gcd.m, create a file (for example: gcdtest.m) and put the following code in it:

test = gcd(40, 50)

然后保存并运行此文件.如果输出无法按预期工作,则重新启动Octave应该可以解决该问题.

Then save and run this file. If the output doesn't work as expected, restarting Octave should fix it.

我选择的数字只是一个例子.

The numbers I've chosen are only an example.

说明:

如果只有函数定义文件(即gcd.m),则在单击保存并运行"时,Octave会自己调用函数,但是它不够聪明,因此不会使用任何参数. .这就是为什么您会收到未定义的变量"错误的原因.这类似于您在测试文件中仅test = gcd().

If all you have is the function definition file (i.e. gcd.m), when you hit "Save and run", Octave will itself call your function, but it's not clever enough and won't use any parameters in doing so. That's why you get an "undefined variable" error. That would be similar to you having only test = gcd() in your test file.

但是,如果您使用参数调用该函数,它们将正确初始化变量xy并且您的代码将起作用.

If, however, you call the function with the arguments, they will initialize the variables x and y correctly and your code will work.

您也可以从八度命令行直接调用gcd(40, 50)进行测试.

You can also simply call gcd(40, 50) from the Octave command line, for testing purposes.

以下是有关函数和函数文件的Octave文档的链接(我知道您说的是您阅读的,但新手可能没有):

The following are links to the Octave documentation regarding functions and function files (I know you said you read them, but newcomers might not have):

https://www.gnu.org/software /octave/doc/interpreter/Defining-Functions.html

https://www.gnu.org/software /octave/doc/interpreter/Function-Files.html

现在,我注意到您的代码中有几个问题:

Now, I noticed a couple of issues in your code:

  • while(r != 0)在第16行-即使在第3行中将r定义为0,并且以后也不会为其分配新值,这将不会运行,甚至不会运行一次.

  • while(r != 0) on line 16 - this won't run, not even once, since you define r as 0 in line 3 and don't assign a new value to it later.

elseif(x<y)(第9行)和else(第12行)都做完全相同的事情.最好完全删除elseif条件,而只使用else.

elseif(x<y) (line 9) and else (line 12) both do exactly the same thing. It would be better to remove the elseif condition entirely and only have else instead.

祝你学习顺利.

这篇关于八度GNU:未定义变量'x',即使已将其定义为函数输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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