简体   繁体   中英

MATLAB/Octave: feval with vector input

I want to put a function in feval(f,x) that has a vector as input eg

function [ ret ] = f (x)
    ret = x(1)^2 - x(2)^2;
end

and

x = [1,2]

but octave always gives an error code:

`x' undefined near line 6 column 18
evaluating argument list element number 1
evaluating argument list element number 1

It seems feval can only evaluate numbers and not vectors. Is there any way to do this?

Create a handle to your function and then call feval on the handle, passing it your vector as its argument:

h = @(x)myfun(x);
x = [1, 2];
y = feval(h, x);

I tried this out in Octave on your function and it seems to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM