matlab not in,MATLAB not enough input arguments

问题

I keep trying to run this and have no idea what is going wrong. I have it saved as test.m. I click run in the editor and in the matlab command window, it states not enough input arguments. I feel like I am missing something totally obvious, but I cannot spot the issue.

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

回答1:

It is a function (not an script) and it needs some input arguments to run (in this case A and x), so you cannot hit the run button and expect it to run.

The first way:

Instead you can use the command windows in MATLAB and enter the command:

A = rand(3,3); % define A here

x = ones(3,1); % define x here

test(A,x) % then run the function with its arguments

remember that A and x should be defined properly.

The second way is:

Also you can hit the little triangle besides the green run button (see the figure below), and it will show you another option, type command to run. And

there you can directly enter the same command test(A,x). After that, each time you just hit enter for this function and it runs this command instead of only the test command without any argument.

67917c5176af621a28f25eb46f290ada.png

回答2:

The third way:

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

This way allows you to "click the play button" and have your function run with no explicit input arguments. However, be advised that such a method should only be used:

When debugging, so as not to allow users to call the function with no arguments if this is not its intended use case.

If your function is not supposed to behave differently for a different number of inputs. See also function overloading in MATLAB based on number of input arguments.

来源:https://stackoverflow.com/questions/21862327/matlab-not-enough-input-arguments

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值