Input a Vector as Function Argument

33 次查看(过去 30 天)
Hi,
I was facing this problem in inputing a vector to my user defined function. I have defined a function that needs two inputs to perform some operations. However, when it try to pass on a vector containing two element to the function it shows need more arguments error.
x = [2 , 4]
y = sq(x)
function out = sq(a , b)
out = a.^2 + b;
end
Can somebody show me how to do it right?
I will be very grateful.
Thanks a lot!

采纳的回答

Alan Stevens
Alan Stevens 2020-12-5
Either
x = [2 , 4]
y = sq(x(1),x(2))
function out = sq(a , b)
out = a.^2 + b;
end
or
x = [2 , 4]
y = sq(x)
function out = sq(x)
a = x(1); b = x(2);
out = a.^2 + b;
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

标签

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by