在Octave中声明函数时未定义的参数

Ami*_*mir 8 function octave

在尝试定义我自己的随机生成器函数时,我得到未定义的变量/参数.

码:

function result = myrand(n, t, p, d)
    a = 200 * t + p
    big_rand = a * n
    result = big_rand / 10**d
    return;
endfunction

mrand = myrand(5379, 0, 91, 4)
Run Code Online (Sandbox Code Playgroud)

错误:

>> myrand
error: 't' undefined near line 2 column 15
error: called from
myrand at line 2 column 7
Run Code Online (Sandbox Code Playgroud)

小智 13

您无法使用function关键字启动脚本. https://www.gnu.org/software/octave/doc/v4.0.1/Script-Files.html

这有效:

disp("Running...")
function result = myrand(n, t, p, d)
     a = 200 * t + p
     big_rand = a * n
     result = big_rand / 10**d
     return;
endfunction

mrand = myrand(5379, 0, 91, 4) 
Run Code Online (Sandbox Code Playgroud)

你应该得到:

warning: function 'myrand' defined within script file 'myrand.m'   
Running ...  
a =  91  
big_rand =  489489  
result =  48.949  
mrand =  48.949  
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,但你知道为什么它会这样工作吗?? (3认同)