MATLAB没有足够的input参数

我一直试图运行这个,不知道什么是错的。 我把它保存为test.m. 我点击编辑器和matlab命令窗口中的运行,它表示没有足够的input参数。 我觉得我错过了一些非常明显的东西,但是我不能发现这个问题。

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 

这是一个函数(不是脚本),它需要一些input参数来运行(在这种情况下, Ax ),所以你不能点击运行button,并期望它运行。

第一种方法:

相反,您可以使用MATLAB中的命令窗口并input命令:

 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应该被正确定义。

第二种方法是:

除了绿色运行button外,你还可以点击小三angular(见下图),它会显示另一个选项, type command to run 。 在那里你可以直接input相同的命令test(A,x) 。 之后,每次你只需要input这个函数,它就会运行这个命令而不是没有任何参数的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 

这种方式可以让你“点击播放button”,让你的function运行,没有明确的input参数。 但是,请注意,只能使用这种方法:

  • debugging的时候 ,为了不让用户在没有参数的情况下调用这个函数。
  • 如果你的函数不应该为不同的input数量而行为不同。 基于input参数的数量,参见MATLAB中的函数重载 。