簡體   English   中英

MATLAB 輸入不夠 arguments

[英]MATLAB not enough input arguments

我一直在嘗試運行它,但不知道出了什么問題。 我把它保存為 test.m。 我在編輯器和 matlab 命令 window 中單擊運行,它指出輸入 arguments 不足。 我覺得我錯過了一些完全明顯的東西,但我無法發現問題。

function y = test(A, x)
    %This function computes the product of matrix A by vector x row-wise
    % define m number of rows here to feed into for loop
    [ma,na] = size(A);
    [mx,nx] = size(x);
    % use if statement to check for proper dimensions
    if(na == mx && nx == 1)
        y = zeros(ma,1);   % initialize y vector 
        for n = 1:ma
            y(n) = A(n,:)*x;
        end
    else
       disp('Dimensions of matrices do not match')
       y = [];
    end
end

它是一個函數(不是腳本),它需要一些輸入參數才能運行(在本例中為Ax ),因此您不能點擊運行按鈕並期望它運行。

第一種方式:

相反,您可以使用 MATLAB 中的命令窗口並輸入命令:

A = rand(3,3); % define A here
x = ones(3,1); % define x here
test(A,x) % then run the function with its arguments

請記住,應正確定義Ax

第二種方式是:

你也可以點擊綠色運行按鈕旁邊的小三角形(見下圖),它會顯示另一個選項, type command to run 在那里你可以直接輸入相同的命令test(A,x) 之后,每次您只需為該函數按下回車鍵,它就會運行此命令,而不僅僅是沒有任何參數的test命令。

在此處輸入圖片說明

第三種方式:

function y = test(A, x)
%// TESTING CODE:
if nargin==0
    A = default_value_for_A;
    x = default_value_for_x;
end
... %// rest of the function code

這種方式允許您“單擊播放按鈕”並讓您的函數在沒有顯式輸入參數的情況下運行。 但是,請注意,這種方法只能用於:

出現此錯誤的原因是因為您從此函數腳本運行代碼。 但是您必須從主腳本(您在其中調用或使用此函數的文件)運行您的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM