2014-02-18 116 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 

这种方式可以让你“点击播放按钮”,并与没有明确的输入参数的功能运行。但是,请注意,才应使用这种方法:

相关问题