Construct a single anonymous function from a cell array of anonymous functions

9 Ansichten (letzte 30 Tage)
I want to run a double loop that creates an anonymous function
h
consisting of the cell array of anonymous functions f . I can do it long-hand, the way that I constructed
g
but that's obviously hopelessly inefficient when f has a large size. I presume I need to use arrayfun but the examples that matlab provides don't help me. Thanks for any suggestions.
Here's my example for how I constructed g.
function nothing
syms a b c d
f{1,1}= @(a,b,c,d) a;
f{1,2}= @(a,b,c,d) b;
f{2,1}= @(a,b,c,d) c;
f{2,2}= @(a,b,c,d) d;
g = @(a,b,c,d)[ f{1,1}(a,b,c,d),f{1,2}(a,b,c,d);f{2,1}(a,b,c,d),f{2,2}(a,b,c,d)];
g(1,2,3,4)
keyboard;

Akzeptierte Antwort

Voss
Voss am 15 Dez. 2021
If f is a cell array of handles to functions that return a scalar result, then you can use cellfun and feval to achieve the result you want:
h = @(a,b,c,d)cellfun(@(x)feval(x,a,b,c,d),f);

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by