简体   繁体   English

MATLAB / Octave:带有向量输入的盛宴

[英]MATLAB/Octave: feval with vector input

I want to put a function in feval(f,x) that has a vector as input eg 我想在feval(f,x)中放置一个以向量为输入的函数,例如

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. 似乎feval只能评估数字而不能评估向量。 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: 创建函数的句柄,然后在该句柄上调用feval ,并将其向量作为参数传递给它:

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. 我在Octave中对您的函数进行了尝试,它似乎可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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