我一直试图运行它并且不知道出了什么问题.我把它保存为test.m. 我在编辑器中单击运行,在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
Run Code Online (Sandbox Code Playgroud)
这是一个函数(不是脚本),它需要一些输入参数来运行(在这种情况下A和x),所以你不能点击运行按钮,并希望它运行.
相反,您可以在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
Run Code Online (Sandbox Code Playgroud)
记住这一点A并且x应该正确定义.
你也可以点击绿色按钮旁边的小三角形(见下图),它会显示另一个选项,type command to run.在那里你可以直接输入相同的命令test(A,x).之后,每次只需按Enter键进入此功能,它就会运行此命令而不是只运行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
Run Code Online (Sandbox Code Playgroud)
这种方式允许您"单击播放按钮"并使您的函数在没有显式输入参数的情况下运行.但是,请注意,只应使用此类方法: