在MATLAB中组合匿名函数 [英] Combining Anonymous Functions in MATLAB

查看:55
本文介绍了在MATLAB中组合匿名函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个匿名函数句柄的单元格数组,想创建一个匿名函数,该函数返回包含每个函数输出的向量.

I have a cell array of anonymous function handles, and would like to create one anonymous function that returns the vector containing the output of each function.

我所拥有的:

ca = {@(X) f(X), @(X)g(X), ...}

我想要什么:

h = @(X) [ca{1}(X), ca{2}(X), ...]

推荐答案

另一种解决方法:

您可以使用 cellfun 将函数应用于每个单元格数组元素,可为您提供带有相应结果的向量.诀窍是应用一个将一些值插入存储在单元格数组中的函数句柄中的函数.

You can use cellfun to apply a function to each cell array element, which gives you a vector with the respective results. The trick is to apply a function that plugs some value into the function handle that is stored in the cell array.

ca = {@(X) X, @(X) X+1, @(X) X^2};
h=@(x) cellfun(@(y) y(x), ca);

给予

>> h(4)

ans =
     4     5    16

这篇关于在MATLAB中组合匿名函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆