简体   繁体   中英

Creating an anonymous function and calling it passing arguments in one line in MATLAB

You can do in matlab something like this:

>> fh = @(x) x^2
fh = 
   @(x)x^2

and then

>> fh(3)
ans =
    9

Now I look for a way to create the anonymous function and call it in one line, like this (it does not work):

@(x) x^2 (3) <-- This code does not work!

Is there a way to do it?

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

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

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

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