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

12 vues (au cours des 30 derniers jours)
Leo Simon le 11 Déc 2021
Commenté : Leo Simon le 15 Déc 2021
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;
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Voss le 15 Déc 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);
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Leo Simon le 15 Déc 2021
Perfect, thanks very much

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by