创建一个匿名函数并在MATLAB中调用它在一行中传递参数

use*_*977 2 matlab function-handle

你可以在matlab中做这样的事情:

>> fh = @(x) x^2
fh = 
   @(x)x^2
Run Code Online (Sandbox Code Playgroud)

然后

>> fh(3)
ans =
    9
Run Code Online (Sandbox Code Playgroud)

现在我寻找一种方法来创建匿名函数并在一行中调用它,就像这样(它不起作用):

@(x) x^2 (3) <-- This code does not work!
Run Code Online (Sandbox Code Playgroud)

有办法吗?

Sam*_*rts 7

feval( @(x) x^2, 3) 是你需要的.


Oli*_*Oli 6

这可以工作(它也适用于矩阵):

arrayfun(@(x) x^2,3)