Input a Vector as Function Argument
51 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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!
0 Kommentare
Akzeptierte Antwort
Alan Stevens
am 5 Dez. 2020
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 Kommentare
Weitere Antworten (1)
Sergey
am 2 Sep. 2024
Use evalc
func = @(a, b) a+b
InputArgumentsNumeric = [1, 2]
InputArgumentsString = strjoin(cellstr(num2str(InputArgumentsNumeric')),',')
[~,output] = evalc("func(" + InputArgumentsString + ")")
0 Kommentare
Siehe auch
Kategorien
Mehr zu Time Series Collections finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!