【问题标题】:Use of anonymous function with feval在 feval 中使用匿名函数
【发布时间】:2013-12-16 02:54:18
【问题描述】:

下面代码中的匿名函数看不懂:

x = 0.25 * randn(3, 1);
y = 0.25 * randn(3, 1);
h = 0.1*randn(3, 1);

interpolate = @(x, y, h, x_new, y_new) ...
    feval(@(int) int(x_new, y_new), ...
          TriScatteredInterp([-1; -1; 1; 1; x], ...
                             [-1; 1; -1; 1; y], ...
                             [0; 0; 0; 0; h]));

我对匿名函数和feval 函数有一些了解,但是我搜索了matlab 文档并没有找到使用多个@ 符号的示例。 feval 参数也有匿名函数。

谁能给点提示?

【问题讨论】:

标签: matlab anonymous-function


【解决方案1】:

所以你可能已经知道这是使用句柄 interpolate 创建一个匿名函数:

interpolate = @(x, y, h, x_new, y_new)...

interpolate 接受这五个输入,并调用feval。现在这里有点棘手,因为feval 本身包含另一个匿名函数。

@(int) int(x_new, y_new),表示接受输入int并返回int(x_new,y_new)的输出。 feval 的附加输入,在本例中为 TriScatteredInterp,被用作该匿名函数的输入。这不是对内置函数 int 的引用(如果您看到以 feval(@int... 开头的内容可能会出现这种情况)。

所以interpolate 所做的基本上等同于这样做,对于任何给定的输入集:

tsi =  TriScatteredInterp([-1; -1; 1; 1; x], ...
                             [-1; 1; -1; 1; y], ...
                             [0; 0; 0; 0; h]));

tsi(x_new,y_new)

您可以通过将tsi(x_new,y_new) 的输出与interpolate(x, y, h, x_new, y_new) 的输出进行比较来测试。

【讨论】:

  • @(int) int(x_new, y_new) 部分,我还是有点困惑。看到匿名函数doc,看到这种风格“sqr = @(x) x.^2;”,所以这里@(int) int(x_new, y_new),第一个“int”应该是参数,后面“ int" 应该是某种类型的操作。但显然不是。怎么样?
  • 两个int引用是相同的。正在执行的操作是返回int(x_new,y_new)的输出,对于任何int(int可以是一个二维矩阵,函数需要两个值等)。匿名函数就是这样灵活的。如果你写f = @(x) x(5),那么f(@ones) 和f(y) 都可以。第一个调用函数ones(5),第二个返回变量y的第5个值。
  • 我有一个问题,TriScattedInterp 的效果? TriScattedInterp 如何根据“(x,y) to h”规则设置函数规则?在这里。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-11
  • 2019-04-04
  • 2013-09-12
  • 1970-01-01
  • 1970-01-01
  • 2017-07-08
  • 1970-01-01
相关资源
最近更新 更多