[英]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
它是一个函数(不是脚本),它需要一些输入参数才能运行(在本例中为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
请记住,应正确定义A
和x
。
你也可以点击绿色运行按钮旁边的小三角形(见下图),它会显示另一个选项, 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.