2016-10-07 5 views
3

私自身のランダムジェネレータ関数を定義しようとすると、未定義の変数/引数が得られます。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) 

エラー:

>> myrand 
error: 't' undefined near line 2 column 15 
error: called from 
myrand at line 2 column 7 
+4

をあなたのコードは、オクターブに私のために動作します。コマンドラインで実行しますか?スクリプトの一部(foo.mなど)の場合は、最初の行が関数宣言で始まらないことを確認してください。一般的な方法は、最初に '1;'を追加することです – Andy

+0

エラーの他に、あなたの "乱数"は単純に '(200 * t * n + p * n)/(10 * d) 'であり、これは4つの変数の単純な1対1の関数です。 – Adriaan

+1

これを 'myrand.m'と* then *' mrand = myrand(5379、0、91、4) 'という名前で保存しましたか、あるいは関数宣言と関数呼び出しをコマンドラインで実行しましたか? (MATLABは、 "この環境では使用できない関数宣言"、Octaveについてはわかりません) – Adriaan

答えて

3

あなたは機能キーワードでスクリプトを起動することはできません。 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) 

あなたが取得する必要があります。

warning: function 'myrand' defined within script file 'myrand.m' 
Running ... 
a = 91 
big_rand = 489489 
result = 48.949 
mrand = 48.949 
関連する問題