2014-02-18 199 views
2

我一直試圖運行這個,不知道發生了什麼問題。我把它保存爲test.m.我點擊編輯器和matlab命令窗口中的運行,說明沒有足夠的輸入參數。我覺得我錯過了一些非常明顯的東西,但我無法發現這個問題。MATLAB沒有足夠的輸入參數

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 
+4

您不能點擊「運行」,因爲它需要參數(「A」和「x」)。你需要輸入'test(A,x)',在那裏你希望定義一些矩陣'A'和'x'。 – JoshG79

+1

這個問題不需要線性代數標籤。 – NKN

回答

7

這是一個函數(不是腳本),它需要一些輸入參數來運行(在這種情況下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命令,沒有任何參數。

enter image description here

5

第三種方法:

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 

這種方式可以讓你「點擊播放按鈕」,並與沒有明確的輸入參數的功能運行。但是,請注意,才應使用這種方法:

相關問題